Messing around with Spring.
authorgumartinm <gu.martinm@gmail.com>
Wed, 9 Nov 2011 15:07:18 +0000 (16:07 +0100)
committergumartinm <gu.martinm@gmail.com>
Wed, 9 Nov 2011 15:07:18 +0000 (16:07 +0100)
Annotations handler and programming with Aspects.
Could be useful or not, but at least it seems interesting.

24 files changed:
AndroidTetris/AndroidManifest.xml
AndroidTetris/src/de/android/androidtetris/DrawView.java
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/pom.xml [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/aop.xml [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/pom.xml [new file with mode: 0644]
SpringJava/SpringAspectJ/pom.xml [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/pom.xml [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/aop.xml [new file with mode: 0644]
SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/spring-config.xml [new file with mode: 0644]

index 217d49f..edd8d88 100644 (file)
@@ -5,9 +5,9 @@
       android:versionName="1.0">
     <uses-sdk android:minSdkVersion="13" />
 
-    <application android:icon="@drawable/icon" android:label="@string/app_name">
+    <application android:label="My AndroidTetris">
         <activity android:name=".AndroidTetrisActivity"
-                  android:label="@string/app_name">
+                  android:theme="@android:style/Theme.NoTitleBar">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
index c51d0f2..d140432 100644 (file)
@@ -8,6 +8,7 @@ import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Color;
+import android.view.KeyEvent;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 
@@ -16,28 +17,21 @@ import android.view.SurfaceView;
  *
  */
 public class DrawView extends SurfaceView {
-       int position = 150;
        private SurfaceHolder holder;
     private AndroidTetrisThread gameLoopThread;
-    
     private static final int TILESIZE=16;
-    //now for the map...
     private static final int MAPWIDTH=10;
     private static final int MAPHEIGHT=30;
     private static final int GREY=8;
-       
-       
        private AndroidTetrisThread thread;
-    
-    Bitmap[] tileArray;
-    Tile[][] mapMatrix;
-    Piece prePiece;
-    Piece currentPiece;
+    private Bitmap[] tileArray;
+    private Tile[][] mapMatrix;
+    private Piece prePiece;
+    private Piece currentPiece;
        
        class AndroidTetrisThread extends Thread 
        {
                private DrawView view;
-
            private boolean running = false;
 
         
@@ -56,20 +50,15 @@ public class DrawView extends SurfaceView {
            {
                while (running) 
                {
-                       Canvas c = null;
-                   try {
-                       c = view.getHolder().lockCanvas();
-                       synchronized (view.getHolder()) 
-                       {
-                               view.move(0, 1);
-                           view.drawMap(c);
-                           //view.onDraw(c);
-                       }
-                   }finally {
-                       if (c != null) 
-                               view.getHolder().unlockCanvasAndPost(c);
-                   }
-                }
+                       Canvas c = view.getHolder().lockCanvas();
+                       synchronized (view.getHolder())
+                       {
+                               view.move(0, 1);
+                               view.drawMap(c);
+                               //view.onDraw(c);
+                       }
+                       view.getHolder().unlockCanvasAndPost(c);
+               }
            }
        }
        
@@ -86,53 +75,53 @@ public class DrawView extends SurfaceView {
     public DrawView(Context context) 
     {
        super(context);
-        this.resetTiles(10);
-        for (Tile color : Tile.values() )
-        {
-               this.loadTile(color.getColor(), color.getColorRGBA());
-        }
-        mapMatrix = new Tile[MAPWIDTH][MAPHEIGHT+1];
-        this.NewGame();
-        this.newBlock();
-       
+       
+        this.newGame();
+        currentPiece = newBlock();
+        currentPiece.x = MAPWIDTH/2-2;
+       currentPiece.y = -1;
+       prePiece = newBlock();
+       prePiece.x=MAPWIDTH+2;
+       prePiece.y=GREY/4;
+       
        // register our interest in hearing about changes to our surface
         //SurfaceHolder holder = getHolder();
         //holder.addCallback(this);
-             gameLoopThread = new AndroidTetrisThread(this);
-             holder = getHolder();
-             holder.addCallback(new SurfaceHolder.Callback() {
-                @Override
-                public void surfaceDestroyed(SurfaceHolder holder) {
-                           boolean retry = true;
-                           gameLoopThread.setRunning(false);
-                           while (retry) {
-                                  try {
-                                        gameLoopThread.join();
-                                        retry = false;
-                                  } catch (InterruptedException e) {
-
-                                  }
-                           }
-                    }
-
-                    @Override
-                    public void surfaceCreated(SurfaceHolder holder) {
-                           gameLoopThread.setRunning(true);
-                           gameLoopThread.start();
-                    }
-
-                    @Override
-                    public void surfaceChanged(SurfaceHolder holder, int format,
-                                       int width, int height) {
-
-                    }
+        gameLoopThread = new AndroidTetrisThread(this);
+        holder = getHolder();
+        holder.addCallback(new SurfaceHolder.Callback() {
+                       @Override
+                       public void surfaceDestroyed(SurfaceHolder holder) {
+                               boolean retry = true;
+                               gameLoopThread.setRunning(false);
+                               while (retry) {
+                                       try {
+                                               gameLoopThread.join();
+                                               retry = false;
+                                       } catch (InterruptedException e) {
+                                       
+                                       }
+                               }
+                       }
+        
+                       @Override
+                       public void surfaceCreated(SurfaceHolder holder) {
+                               gameLoopThread.setRunning(true);
+                               gameLoopThread.start();
+                       }
+        
+                       @Override
+                       public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
+               
+                       }
+        });      
+    }
 
-             });
-       }
     
     public void resetTiles(int tilecount) {
        tileArray = new Bitmap[tilecount];
     }
+
     
     public void loadTile(int key, int color)
     {
@@ -145,9 +134,17 @@ public class DrawView extends SurfaceView {
        }
            tileArray[key] = bitmap;
     }
+
     
-    protected void NewGame()
+    protected void newGame()
     {
+        this.resetTiles(10);
+         for (Tile color : Tile.values() )
+         {
+               this.loadTile(color.getColor(), color.getColorRGBA());
+         }
+         mapMatrix = new Tile[MAPWIDTH][MAPHEIGHT+1];
+       
        //start out the map
        for(int x=0;x< MAPWIDTH;x++)
        {
@@ -158,17 +155,13 @@ public class DrawView extends SurfaceView {
        }
     }
     
-    protected void newBlock()
+    protected Piece newBlock()
     {
        Random random = new Random();
                
-       currentPiece = Piece.getPiece(random.nextInt(7)%7);
-       currentPiece.x = MAPWIDTH/2-2;
-       currentPiece.y = -1;
+       Piece piece = Piece.getPiece(random.nextInt(7)%7);
        
-       prePiece = Piece.getPiece(random.nextInt(7)%7);
-       prePiece.x=MAPWIDTH+2;
-       prePiece.y=GREY/4;
+       return piece;
     }
     
     protected void drawTile(Canvas canvas, int color, int x, int y)
@@ -207,7 +200,12 @@ public class DrawView extends SurfaceView {
     {
        if (this.collisionTest(x, y))
        {
-               this.newBlock();
+               currentPiece = prePiece;
+            currentPiece.x = MAPWIDTH/2-2;
+               currentPiece.y = -1;
+               prePiece = newBlock();
+               prePiece.x=MAPWIDTH+2;
+               prePiece.y=GREY/4;
        }
        else
        {
@@ -239,6 +237,30 @@ public class DrawView extends SurfaceView {
        return false;
     }
     
+    
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent msg) {
+       if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
+               try {
+                               AndroidTetrisThread.sleep(1000000);
+                       } catch (InterruptedException e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       }
+                Canvas c =null;
+                c = this.getHolder().lockCanvas();
+                synchronized (this.getHolder()) 
+             {
+               this.move(1, 0);
+                this.drawMap(c);
+                //view.onDraw(c);
+             }
+             return true;
+        }
+       return false;
+    }
+    
+    
     @Override
     protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/pom.xml b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/pom.xml
new file mode 100644 (file)
index 0000000..dab88d4
--- /dev/null
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+       xmlns="http://maven.apache.org/POM/4.0.0" 
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+       <modelVersion>4.0.0</modelVersion>
+
+       <parent>
+               <artifactId>custom-annotations</artifactId>
+               <groupId>de.spring.example</groupId>
+               <version>2.0-SNAPSHOT</version>
+       </parent>
+
+       <groupId>de.spring.example</groupId>
+       <artifactId>custom-annotations-implementation</artifactId>
+       <version>2.0-SNAPSHOT</version>
+       <name>custom-annotations-implementation</name>
+       <url>http://maven.apache.org</url>
+
+       <dependencies>
+       
+               <!-- Assembly version info -->
+               <dependency>
+                       <groupId>org.apache.maven</groupId>
+                       <artifactId>maven-model</artifactId>
+                       <version>2.2.1</version>        
+               </dependency>
+
+               <!--  
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-aspects</artifactId>
+                       <version>2.5.6</version>
+               </dependency>
+               -->
+
+               <dependency>
+            <groupId>org.springframework</groupId>
+                       <artifactId>spring-context</artifactId>
+               <version>2.5.6</version>
+               </dependency>
+
+               <dependency>
+             <groupId>org.springframework</groupId>
+             <artifactId>spring-jdbc</artifactId>
+               <version>2.5.6</version>
+               </dependency>   
+
+               <dependency>
+              <groupId>c3p0</groupId>
+              <artifactId>c3p0</artifactId>
+               <version>0.9.1.2</version>
+        </dependency>
+                
+        <dependency>
+                       <groupId>org.aspectj</groupId>
+                       <artifactId>aspectjrt</artifactId>
+                       <version>1.6.12</version>
+               </dependency>
+
+               <dependency>
+                       <groupId>org.aspectj</groupId>
+                       <artifactId>aspectjweaver</artifactId>
+                       <version>1.6.12</version>
+               </dependency>
+                
+         <!--  
+         <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-agent</artifactId>
+                       <version>2.5.6</version>
+                </dependency>
+               -->
+
+       </dependencies>
+
+</project>
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java
new file mode 100644 (file)
index 0000000..034a7a3
--- /dev/null
@@ -0,0 +1,33 @@
+package de.spring.example;
+
+import java.util.Map;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+
+public class AnnotationsHandler implements ApplicationContextAware, InitializingBean {
+         private ApplicationContext applicationContext;
+          
+         @Override
+         public void afterPropertiesSet() {
+                 GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext);
+                 
+                 final Map<String, Object> transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(TransactionalN2A.class);
+
+                 for (final Object myFoo : transactionalClass.values()) {
+                         final Class<? extends Object> fooClass = myFoo.getClass();
+                         final TransactionalN2A annotation = fooClass.getAnnotation(TransactionalN2A.class);
+                         System.out.println("Found 1 foo class: " + fooClass + ", with tags: ");
+                 }
+         }
+
+         @Override
+         public void setApplicationContext(final ApplicationContext applicationContext)
+             throws BeansException {
+           this.applicationContext = applicationContext;
+         }
+       }
+
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java
new file mode 100644 (file)
index 0000000..0f0da3f
--- /dev/null
@@ -0,0 +1,19 @@
+package de.spring.example;
+
+import java.util.Map;
+
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+@Aspect
+public class AspectHandler {
+        
+       
+          
+       
+       }
+
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java
new file mode 100644 (file)
index 0000000..548c5e9
--- /dev/null
@@ -0,0 +1,17 @@
+package de.spring.example;
+
+
+public class Prueba {
+  public void bar(){
+    System.out.println("I am not a number, I am a free man!");
+  }
+  
+  @TransactionalN2A
+  public class InnerService {
+      public void innerMethod() {
+          System.out.println("xxx: AopService$InnerClass.innerMethod()");
+      }
+  }
+
+}
+
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java
new file mode 100644 (file)
index 0000000..cc452f6
--- /dev/null
@@ -0,0 +1,58 @@
+package de.spring.example;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+/**
+ * Localizador de beans para de los dispositivos
+ *
+ * @author rvp001es
+ */
+public final class SpringContextLocator {
+
+
+       // Singleton Pattern
+       private static SpringContextLocator instance;
+
+       // Spring ApplicationContext
+       private static ApplicationContext context;
+
+       // Dispositivos logicos
+       private static final String SPRING_CONFIG_CONTEXT="spring-config.xml";
+       //private static final String DATABASE_CONFIG="database-config.xml";
+
+       
+       /**
+        * Private constructor. Singleton pattern.
+        */
+       private SpringContextLocator() {
+               String[] factoryFiles = null;
+               System.out.println("Loading files context " + 
+                                                                               SpringContextLocator.SPRING_CONFIG_CONTEXT);
+
+               factoryFiles = new String[] { SPRING_CONFIG_CONTEXT };
+               
+               SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles);
+
+               System.out.println("The N2A devices context and test " +
+                                                                               "context has been loaded successfully!! ");
+       }
+
+       /**
+        * Singleton pattern. GetInstance()
+        */
+       public synchronized static SpringContextLocator getInstance() {
+               if (SpringContextLocator.instance == null) {
+                       SpringContextLocator.instance = new SpringContextLocator();
+               }
+               return SpringContextLocator.instance;
+       }
+
+       /**
+        * Return a bean in application context.
+        */
+       public Object getBean(final String name) {
+               return SpringContextLocator.context.getBean(name);
+       }
+}
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java
new file mode 100644 (file)
index 0000000..b566806
--- /dev/null
@@ -0,0 +1,14 @@
+package de.spring.example;
+
+public class SpringStart {
+
+       /**
+        * @param args
+        */
+       public static void main(String[] args) {
+               System.out.println("HOLA");
+               SpringContextLocator.getInstance();
+
+       }
+
+}
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java
new file mode 100644 (file)
index 0000000..30e9b66
--- /dev/null
@@ -0,0 +1,41 @@
+package de.spring.example;
+
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+
+
+public class TransactionManagerN2A {
+       private static TransactionManagerN2A instance = new TransactionManagerN2A();
+       private DataSourceTransactionManager transactionManager;
+       private TransactionStatus transactionStatus;
+
+       //Why could you want to extend this class?
+       private TransactionManagerN2A() {}
+       
+       public static TransactionManagerN2A getInstance() {
+        return instance;
+       }
+       
+       public void initTransaction()
+       {
+               transactionStatus = this.transactionManager.getTransaction(null);
+       }
+       
+       public void rollbackTransaction()
+       {
+               this.transactionManager.rollback(this.transactionStatus);
+       }
+       
+       public void commitTransaction()
+       {
+               this.transactionManager.commit(this.transactionStatus);
+       }
+       
+       
+       /************************* Setters and getters *******************************************/
+       public void setTransactionManager(final DataSourceTransactionManager  transactionManager) 
+       {
+               this.transactionManager = transactionManager;
+       }
+}
+
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java
new file mode 100644 (file)
index 0000000..474dc63
--- /dev/null
@@ -0,0 +1,14 @@
+/**
+ * 
+ */
+package de.spring.example;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import org.springframework.stereotype.Component;
+
+
+@Retention(RetentionPolicy.RUNTIME)
+@Component
+public @interface TransactionalN2A {
+}
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/aop.xml b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/aop.xml
new file mode 100644 (file)
index 0000000..12e9571
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE aspectj PUBLIC
+        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
+       <aspectj>
+       <weaver options="-verbose -showWeaveInfo"/>
+       <!-- only weave classes in our application-specific packages -->
+               <include within="es.dia.*"/>
+        </weaver>
+               <aspects>
+               <aspect name="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler"/>        
+       </aspects>
+       </aspectj>
\ No newline at end of file
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml
new file mode 100644 (file)
index 0000000..6ea229c
--- /dev/null
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context" 
+       xmlns:aop="http://www.springframework.org/schema/aop"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans 
+                                                  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+                                                  http://www.springframework.org/schema/aop 
+                                                  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
+                                                  http://www.springframework.org/schema/context 
+                                                  http://www.springframework.org/schema/context/spring-context-2.5.xsd
+                                                  http://www.springframework.org/schema/tx 
+                                                  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
+  
+     <aop:aspectj-autoproxy proxy-target-class="true"/>
+  
+        <context:load-time-weaver
+               weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"
+               aspectj-weaving="on"/>
+
+
+    <context:component-scan base-package="es.dia">
+       <context:include-filter 
+       type="annotation" 
+       expression="es.dia.pos.n2a.aspectj.annotations.TransactionalN2A" />
+       </context:component-scan>
+
+
+       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
+                <property name="user" value="root"/>
+                <property name="password" value=""/>
+                <property name="driverClass" value="com.mysql.jdbc.Driver"/>
+                <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/n2a?autoReconnect=true"/>
+                <property name="initialPoolSize" value="5"/>
+                <property name="maxPoolSize" value="20"/>
+                <property name="minPoolSize" value="10"/>
+                <property name="acquireIncrement" value="1"/>
+                <property name="acquireRetryAttempts" value="5"/>
+                <property name="acquireRetryDelay" value="1000"/>
+                <property name="automaticTestTable" value="con_test"/>
+                <property name="checkoutTimeout" value="5000"/>
+       </bean>
+       
+       <bean id="annotationsHandler" class="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler" >
+       
+       </bean>
+       
+       <!--  
+       <bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
+        <property name="transactionManagerN2A" ref="transactionManagerN2A"/>
+    </bean>    
+    -->
+    
+    <bean id="pruebaOuter" class="es.dia.pos.n2a.aspectj.annotations.Prueba"/>
+    <bean id="pruebaInner" class="es.dia.pos.n2a.aspectj.annotations.Prueba$InnerService">
+        <constructor-arg ref="pruebaOuter"/>
+    </bean>
+       
+       <!--
+               I do not care about the N2A default configuration, so I use here the scope singleton and lazy-init false.
+       -->
+       <bean id="transactionManagerN2A" 
+              class="es.dia.pos.n2a.aspectj.annotations.TransactionManagerN2A"
+              factory-method="getInstance"
+              scope="singleton"
+              lazy-init="false" >
+        <property name="transactionManager">
+            <ref bean="transactionManager" />
+        </property>
+    </bean>
+    
+    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+       <property name="dataSource" ref="dataSource"/>
+    </bean>
+
+</beans>
diff --git a/SpringJava/AnnotationsCustomHandler/pom.xml b/SpringJava/AnnotationsCustomHandler/pom.xml
new file mode 100644 (file)
index 0000000..fa98657
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <groupId>de.spring.example</groupId>
+       <artifactId>custom-annotations</artifactId>
+       <version>2.0-SNAPSHOT</version>
+       <name>custom-annotations</name>
+       <url>http://maven.apache.org</url>
+       <packaging>pom</packaging>
+
+       <dependencies>
+       </dependencies>
+
+       <modules>
+          <module>custom-annotations-implementation</module>
+       </modules>
+</project>
diff --git a/SpringJava/SpringAspectJ/pom.xml b/SpringJava/SpringAspectJ/pom.xml
new file mode 100644 (file)
index 0000000..3e29b6f
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <groupId>de.spring.example</groupId>
+       <artifactId>spring-aspectj</artifactId>
+       <version>2.0-SNAPSHOT</version>
+       <name>custom-annotations</name>
+       <url>http://maven.apache.org</url>
+       <packaging>pom</packaging>
+
+       <dependencies>
+       </dependencies>
+
+       <modules>
+          <module>spring-aspectj-example</module>
+       </modules>
+</project>
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/pom.xml b/SpringJava/SpringAspectJ/spring-aspectj-example/pom.xml
new file mode 100644 (file)
index 0000000..0aef4e8
--- /dev/null
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+       xmlns="http://maven.apache.org/POM/4.0.0" 
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+       <modelVersion>4.0.0</modelVersion>
+
+       <parent>
+               <artifactId>spring-aspectj</artifactId>
+               <groupId>de.spring.example</groupId>
+               <version>2.0-SNAPSHOT</version>
+       </parent>
+
+       <groupId>de.spring.example</groupId>
+       <artifactId>spring-aspectj-example</artifactId>
+       <version>2.0-SNAPSHOT</version>
+       <name>spring-aspectj-example</name>
+       <url>http://maven.apache.org</url>
+
+       <dependencies>
+       
+               <!-- Assembly version info -->
+               <dependency>
+                       <groupId>org.apache.maven</groupId>
+                       <artifactId>maven-model</artifactId>
+                       <version>2.2.1</version>        
+               </dependency>
+
+               <!--  
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-aspects</artifactId>
+                       <version>2.5.6</version>
+               </dependency>
+               -->
+
+               <dependency>
+            <groupId>org.springframework</groupId>
+                       <artifactId>spring-context</artifactId>
+               <version>2.5.6</version>
+               </dependency>
+
+               <dependency>
+             <groupId>org.springframework</groupId>
+             <artifactId>spring-jdbc</artifactId>
+               <version>2.5.6</version>
+               </dependency>   
+
+               <dependency>
+              <groupId>c3p0</groupId>
+              <artifactId>c3p0</artifactId>
+               <version>0.9.1.2</version>
+        </dependency>
+                
+        <dependency>
+                       <groupId>org.aspectj</groupId>
+                       <artifactId>aspectjrt</artifactId>
+                       <version>1.6.12</version>
+               </dependency>
+
+               <dependency>
+                       <groupId>org.aspectj</groupId>
+                       <artifactId>aspectjweaver</artifactId>
+                       <version>1.6.12</version>
+               </dependency>
+                
+         <!--  
+         <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-agent</artifactId>
+                       <version>2.5.6</version>
+                </dependency>
+               -->
+
+       </dependencies>
+
+</project>
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java
new file mode 100644 (file)
index 0000000..034a7a3
--- /dev/null
@@ -0,0 +1,33 @@
+package de.spring.example;
+
+import java.util.Map;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+
+public class AnnotationsHandler implements ApplicationContextAware, InitializingBean {
+         private ApplicationContext applicationContext;
+          
+         @Override
+         public void afterPropertiesSet() {
+                 GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext);
+                 
+                 final Map<String, Object> transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(TransactionalN2A.class);
+
+                 for (final Object myFoo : transactionalClass.values()) {
+                         final Class<? extends Object> fooClass = myFoo.getClass();
+                         final TransactionalN2A annotation = fooClass.getAnnotation(TransactionalN2A.class);
+                         System.out.println("Found 1 foo class: " + fooClass + ", with tags: ");
+                 }
+         }
+
+         @Override
+         public void setApplicationContext(final ApplicationContext applicationContext)
+             throws BeansException {
+           this.applicationContext = applicationContext;
+         }
+       }
+
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java
new file mode 100644 (file)
index 0000000..0f0da3f
--- /dev/null
@@ -0,0 +1,19 @@
+package de.spring.example;
+
+import java.util.Map;
+
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+@Aspect
+public class AspectHandler {
+        
+       
+          
+       
+       }
+
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java
new file mode 100644 (file)
index 0000000..548c5e9
--- /dev/null
@@ -0,0 +1,17 @@
+package de.spring.example;
+
+
+public class Prueba {
+  public void bar(){
+    System.out.println("I am not a number, I am a free man!");
+  }
+  
+  @TransactionalN2A
+  public class InnerService {
+      public void innerMethod() {
+          System.out.println("xxx: AopService$InnerClass.innerMethod()");
+      }
+  }
+
+}
+
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java
new file mode 100644 (file)
index 0000000..cc452f6
--- /dev/null
@@ -0,0 +1,58 @@
+package de.spring.example;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+/**
+ * Localizador de beans para de los dispositivos
+ *
+ * @author rvp001es
+ */
+public final class SpringContextLocator {
+
+
+       // Singleton Pattern
+       private static SpringContextLocator instance;
+
+       // Spring ApplicationContext
+       private static ApplicationContext context;
+
+       // Dispositivos logicos
+       private static final String SPRING_CONFIG_CONTEXT="spring-config.xml";
+       //private static final String DATABASE_CONFIG="database-config.xml";
+
+       
+       /**
+        * Private constructor. Singleton pattern.
+        */
+       private SpringContextLocator() {
+               String[] factoryFiles = null;
+               System.out.println("Loading files context " + 
+                                                                               SpringContextLocator.SPRING_CONFIG_CONTEXT);
+
+               factoryFiles = new String[] { SPRING_CONFIG_CONTEXT };
+               
+               SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles);
+
+               System.out.println("The N2A devices context and test " +
+                                                                               "context has been loaded successfully!! ");
+       }
+
+       /**
+        * Singleton pattern. GetInstance()
+        */
+       public synchronized static SpringContextLocator getInstance() {
+               if (SpringContextLocator.instance == null) {
+                       SpringContextLocator.instance = new SpringContextLocator();
+               }
+               return SpringContextLocator.instance;
+       }
+
+       /**
+        * Return a bean in application context.
+        */
+       public Object getBean(final String name) {
+               return SpringContextLocator.context.getBean(name);
+       }
+}
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java
new file mode 100644 (file)
index 0000000..b566806
--- /dev/null
@@ -0,0 +1,14 @@
+package de.spring.example;
+
+public class SpringStart {
+
+       /**
+        * @param args
+        */
+       public static void main(String[] args) {
+               System.out.println("HOLA");
+               SpringContextLocator.getInstance();
+
+       }
+
+}
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java
new file mode 100644 (file)
index 0000000..30e9b66
--- /dev/null
@@ -0,0 +1,41 @@
+package de.spring.example;
+
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+
+
+public class TransactionManagerN2A {
+       private static TransactionManagerN2A instance = new TransactionManagerN2A();
+       private DataSourceTransactionManager transactionManager;
+       private TransactionStatus transactionStatus;
+
+       //Why could you want to extend this class?
+       private TransactionManagerN2A() {}
+       
+       public static TransactionManagerN2A getInstance() {
+        return instance;
+       }
+       
+       public void initTransaction()
+       {
+               transactionStatus = this.transactionManager.getTransaction(null);
+       }
+       
+       public void rollbackTransaction()
+       {
+               this.transactionManager.rollback(this.transactionStatus);
+       }
+       
+       public void commitTransaction()
+       {
+               this.transactionManager.commit(this.transactionStatus);
+       }
+       
+       
+       /************************* Setters and getters *******************************************/
+       public void setTransactionManager(final DataSourceTransactionManager  transactionManager) 
+       {
+               this.transactionManager = transactionManager;
+       }
+}
+
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java
new file mode 100644 (file)
index 0000000..474dc63
--- /dev/null
@@ -0,0 +1,14 @@
+/**
+ * 
+ */
+package de.spring.example;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import org.springframework.stereotype.Component;
+
+
+@Retention(RetentionPolicy.RUNTIME)
+@Component
+public @interface TransactionalN2A {
+}
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/aop.xml b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/aop.xml
new file mode 100644 (file)
index 0000000..12e9571
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE aspectj PUBLIC
+        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
+       <aspectj>
+       <weaver options="-verbose -showWeaveInfo"/>
+       <!-- only weave classes in our application-specific packages -->
+               <include within="es.dia.*"/>
+        </weaver>
+               <aspects>
+               <aspect name="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler"/>        
+       </aspects>
+       </aspectj>
\ No newline at end of file
diff --git a/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/spring-config.xml b/SpringJava/SpringAspectJ/spring-aspectj-example/src/main/resources/spring-config.xml
new file mode 100644 (file)
index 0000000..6ea229c
--- /dev/null
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context" 
+       xmlns:aop="http://www.springframework.org/schema/aop"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans 
+                                                  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+                                                  http://www.springframework.org/schema/aop 
+                                                  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
+                                                  http://www.springframework.org/schema/context 
+                                                  http://www.springframework.org/schema/context/spring-context-2.5.xsd
+                                                  http://www.springframework.org/schema/tx 
+                                                  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
+  
+     <aop:aspectj-autoproxy proxy-target-class="true"/>
+  
+        <context:load-time-weaver
+               weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"
+               aspectj-weaving="on"/>
+
+
+    <context:component-scan base-package="es.dia">
+       <context:include-filter 
+       type="annotation" 
+       expression="es.dia.pos.n2a.aspectj.annotations.TransactionalN2A" />
+       </context:component-scan>
+
+
+       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
+                <property name="user" value="root"/>
+                <property name="password" value=""/>
+                <property name="driverClass" value="com.mysql.jdbc.Driver"/>
+                <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/n2a?autoReconnect=true"/>
+                <property name="initialPoolSize" value="5"/>
+                <property name="maxPoolSize" value="20"/>
+                <property name="minPoolSize" value="10"/>
+                <property name="acquireIncrement" value="1"/>
+                <property name="acquireRetryAttempts" value="5"/>
+                <property name="acquireRetryDelay" value="1000"/>
+                <property name="automaticTestTable" value="con_test"/>
+                <property name="checkoutTimeout" value="5000"/>
+       </bean>
+       
+       <bean id="annotationsHandler" class="es.dia.pos.n2a.aspectj.annotations.AnnotationsHandler" >
+       
+       </bean>
+       
+       <!--  
+       <bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
+        <property name="transactionManagerN2A" ref="transactionManagerN2A"/>
+    </bean>    
+    -->
+    
+    <bean id="pruebaOuter" class="es.dia.pos.n2a.aspectj.annotations.Prueba"/>
+    <bean id="pruebaInner" class="es.dia.pos.n2a.aspectj.annotations.Prueba$InnerService">
+        <constructor-arg ref="pruebaOuter"/>
+    </bean>
+       
+       <!--
+               I do not care about the N2A default configuration, so I use here the scope singleton and lazy-init false.
+       -->
+       <bean id="transactionManagerN2A" 
+              class="es.dia.pos.n2a.aspectj.annotations.TransactionManagerN2A"
+              factory-method="getInstance"
+              scope="singleton"
+              lazy-init="false" >
+        <property name="transactionManager">
+            <ref bean="transactionManager" />
+        </property>
+    </bean>
+    
+    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+       <property name="dataSource" ref="dataSource"/>
+    </bean>
+
+</beans>