aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lin...@apache.org
Subject svn commit: r941492 - in /incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl: ./ src/main/java/org/apache/aries/blueprint/annotation/impl/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/ar...
Date Wed, 05 May 2010 21:12:32 GMT
Author: linsun
Date: Wed May  5 21:12:31 2010
New Revision: 941492

URL: http://svn.apache.org/viewvc?rev=941492&view=rev
Log:
[blueprint annotation] update implementation of annotation scanning

Added:
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java
  (with props)
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
  (with props)
Modified:
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/pom.xml
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/Activator.java
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/pom.xml?rev=941492&r1=941491&r2=941492&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/pom.xml (original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/pom.xml Wed May  5
21:12:31 2010
@@ -37,10 +37,11 @@
           org.apache.aries.blueprint.annotation.impl.Activator
       </aries.osgi.activator>
       <aries.osgi.export.pkg>
-          org.apache.aries.blueprint.jaxb.*, org.aoache.aries.blueprint.annotation.*
+          org.apache.aries.blueprint.jaxb.*, org.apache.aries.blueprint.annotation.impl
       </aries.osgi.export.pkg>
       <aries.osgi.import>
           !org.apache.aries.blueprint.jaxb.*,
+          !org.apache.aries.blueprint.annotation.impl,
           org.apache.aries.blueprint.annotation,
         *
       </aries.osgi.import>

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/Activator.java?rev=941492&r1=941491&r2=941492&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/Activator.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/Activator.java
Wed May  5 21:12:31 2010
@@ -16,8 +16,12 @@
  */
 package org.apache.aries.blueprint.annotation.impl;
 
+import java.util.Dictionary;
+import java.util.Hashtable;
+
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 import org.apache.aries.blueprint.annotation.service.BlueprintAnnotationScanner;
 
@@ -27,7 +31,10 @@ public class Activator implements Bundle
     
     public void start(BundleContext context) {
         System.out.println("Annotation Scanner Impl Bundle start");
-        sr = context.registerService(BlueprintAnnotationScanner.class.getName(), new BlueprintAnnotationScannerImpl(context),
null);
+        Dictionary dict = new Hashtable();
+        dict.put(Constants.SERVICE_RANKING, 0);
+        sr = context.registerService(BlueprintAnnotationScanner.class.getName(), new BlueprintAnnotationScannerImpl(context),
dict);
+        System.out.println("finish register service");
     }
 
     public void stop(BundleContext context) {

Added: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java?rev=941492&view=auto
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java
(added)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java
Wed May  5 21:12:31 2010
@@ -0,0 +1,35 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.aries.blueprint.annotation.impl;
+
+public class BlueprintAnnotationException extends RuntimeException {
+    public BlueprintAnnotationException() {
+    }
+
+    public BlueprintAnnotationException(String message) {
+        super(message);
+    }
+
+    public BlueprintAnnotationException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public BlueprintAnnotationException(Throwable cause) {
+        super(cause);
+    }
+
+}

Propchange: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java?rev=941492&r1=941491&r2=941492&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java
Wed May  5 21:12:31 2010
@@ -16,11 +16,18 @@
  */
 package org.apache.aries.blueprint.annotation.impl;
 
+import java.io.DataInputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.math.BigInteger;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Arrays;
 import java.util.List;
 
 import javax.xml.bind.JAXBContext;
@@ -28,9 +35,18 @@ import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 
 import org.apache.xbean.finder.BundleAnnotationFinder;
+import org.apache.aries.blueprint.annotation.impl.BlueprintAnnotationException;
 import org.apache.aries.blueprint.annotation.service.BlueprintAnnotationScanner;
 import org.apache.aries.blueprint.jaxb.Tbean;
+import org.apache.aries.blueprint.jaxb.Tdescription;
 import org.apache.aries.blueprint.jaxb.Tproperty;
+import org.apache.aries.blueprint.jaxb.Tref;
+import org.apache.aries.blueprint.jaxb.Treference;
+import org.apache.aries.blueprint.jaxb.TreferenceList;
+import org.apache.aries.blueprint.jaxb.TreferenceListener;
+import org.apache.aries.blueprint.jaxb.TregistrationListener;
+import org.apache.aries.blueprint.jaxb.Tservice;
+import org.apache.aries.blueprint.jaxb.TtypeConverters;
 import org.apache.aries.blueprint.jaxb.Tvalue;
 import org.apache.aries.blueprint.jaxb.Tblueprint;
 import org.osgi.framework.Bundle;
@@ -38,144 +54,456 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.apache.aries.blueprint.annotation.Bean;
 import org.apache.aries.blueprint.annotation.Blueprint;
+import org.apache.aries.blueprint.annotation.Destroy;
+import org.apache.aries.blueprint.annotation.Init;
 import org.apache.aries.blueprint.annotation.Inject;
+import org.apache.aries.blueprint.annotation.Reference;
+import org.apache.aries.blueprint.annotation.ReferenceList;
+import org.apache.aries.blueprint.annotation.ReferenceListener;
+import org.apache.aries.blueprint.annotation.RegistrationListener;
+import org.apache.aries.blueprint.annotation.Service;
+import org.osgi.service.blueprint.container.Converter;
 import org.osgi.service.packageadmin.PackageAdmin;
 
-public class BlueprintAnnotationScannerImpl implements BlueprintAnnotationScanner {
+public class BlueprintAnnotationScannerImpl implements
+        BlueprintAnnotationScanner {
     private BundleContext context;
-    private List<Class> classes;
-    
+
     public BlueprintAnnotationScannerImpl(BundleContext bc) {
-        this.context = bc;    
+        this.context = bc;
     }
-    public boolean foundBlueprintAnnotation(Bundle bundle) {
-        if (this.classes != null && !this.classes.isEmpty()) {
-            return true;
+
+    private BundleContext getBlueprintExtenderContext() {
+        Bundle[] bundles = this.context.getBundles();
+        for (Bundle b : bundles) {
+            if (b.getSymbolicName().equals("org.apache.aries.blueprint.core")) {
+                return b.getBundleContext();
+            }
         }
+
+        return null;
+    }
+
+    private BundleAnnotationFinder createBundleAnnotationFinder(Bundle bundle) {
         ServiceReference sr = this.context.getServiceReference(PackageAdmin.class.getName());
-        PackageAdmin pa = (PackageAdmin)this.context.getService(sr);
+        PackageAdmin pa = (PackageAdmin) this.context.getService(sr);
         BundleAnnotationFinder baf = null;
         try {
             baf = new BundleAnnotationFinder(pa, bundle);
-            // locate the classes that has @blueprint annotation
-            this.classes = baf.findAnnotatedClasses(Blueprint.class);
-            
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
-        
 
         this.context.ungetService(sr);
-        if (this.classes == null || this.classes.isEmpty()) {
-            return false;
-        }
         
-        return true;
+        return baf;
     }
+    
+    public URL createBlueprintModel(Bundle bundle) {
 
-    private BundleContext getBlueprintExtenderContext() {
-        Bundle[] bundles = this.context.getBundles();
-        for (Bundle b : bundles) {
-            if (b.getSymbolicName().equals("org.apache.aries.blueprint.core")) {
-                return b.getBundleContext();
+        Tblueprint tblueprint = generateBlueprintModel(bundle);
+
+        if (tblueprint != null) {
+            // create the generated blueprint xml file in bundle storage
+            // area
+            BundleContext ctx = getBlueprintExtenderContext();
+
+            if (ctx == null) {
+                // blueprint extender doesn't exist, let' still generate the
+                // bundle, using the bundle's bundle context
+                ctx = bundle.getBundleContext();
             }
-        }
-        
-        throw new NullPointerException("unable to get bundle context for aries blueprint
extender");
-    }
-    
-    public URL generateBlueprintModel(Bundle bundle) {
-        if (foundBlueprintAnnotation(bundle)) {
-            Tblueprint tblueprint = generateBlueprintModel(classes);
-            
-            if (tblueprint != null) {
-                // create the generated blueprint xml file in bundle storage area
-                BundleContext extenderContext = getBlueprintExtenderContext();
-                File dir = extenderContext.getDataFile(bundle.getSymbolicName() + "/" + bundle.getVersion()
+ "/");
-                if (!dir.exists()) {
-                    dir.mkdirs();
-                }
-                String blueprintPath = cachePath(bundle, "annotation-generated-blueprint.xml");
-                File file = extenderContext.getDataFile(blueprintPath);
-                if (!file.exists()) {
-                    try {
-                        file.createNewFile();
-                    } catch (IOException e) {
-                        // TODO Auto-generated catch block
-                        e.printStackTrace();
-                    }
-                }
-                try {
-                    marshallOBRModel(tblueprint, file);
-                } catch (JAXBException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                }
+
+            File dir = ctx.getDataFile(bundle.getSymbolicName() + "/"
+                    + bundle.getVersion() + "/");
+            if (!dir.exists()) {
+                dir.mkdirs();
+            }
+            String blueprintPath = cachePath(bundle,
+                    "annotation-generated-blueprint.xml");
+            File file = ctx.getDataFile(blueprintPath);
+            if (!file.exists()) {
                 try {
-                    return file.toURL();
-                } catch (MalformedURLException e) {
+                    file.createNewFile();
+                } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
+            try {
+                marshallOBRModel(tblueprint, file);
+            } catch (JAXBException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
             
+            System.out.println("generated annotation xml is located " + file.getAbsolutePath());
+            try {
+                return file.toURL();
+            } catch (MalformedURLException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
         }
-        
+
         return null;
 
     }
 
-    private void marshallOBRModel(Tblueprint tblueprint, File blueprintFile) throws JAXBException
{
+    private void marshallOBRModel(Tblueprint tblueprint, File blueprintFile)
+            throws JAXBException {
         JAXBContext context = JAXBContext.newInstance(Tblueprint.class);
         Marshaller marshaller = context.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
         marshaller.marshal(tblueprint, blueprintFile);
-        
+
     }
 
-    
-    private Tblueprint generateBlueprintModel(List<Class> classes) {
-        if (classes.isEmpty()) {
-            return null;
-        }
+    private Tblueprint generateBlueprintModel(Bundle bundle) {
+        BundleAnnotationFinder baf = createBundleAnnotationFinder(bundle);
+
+        List<Class> blueprintClasses = baf.findAnnotatedClasses(Blueprint.class);
+        List<Class> beanClasses = baf.findAnnotatedClasses(Bean.class);
+        List<Class> refClasses = baf.findAnnotatedClasses(Reference.class);
+        List<Class> refListClasses = baf.findAnnotatedClasses(ReferenceList.class);
         
         Tblueprint tblueprint = new Tblueprint();
+        
+        
+        if (!blueprintClasses.isEmpty()) {
+            // use the first annotated blueprint annotation
+            Blueprint blueprint = (Blueprint)blueprintClasses.get(0).getAnnotation(Blueprint.class);
+            tblueprint.setDefaultActivation(blueprint.defaultActivation());
+            tblueprint.setDefaultAvailability(blueprint.defaultAvailability());
+            tblueprint.setDefaultTimeout(convertToBigInteger(blueprint.defaultTimeout()));
+        }
+
         List<Object> components = tblueprint.getServiceOrReferenceListOrBean();
-        for (Class clazz : classes) {
-            if (clazz.isAnnotationPresent(Bean.class)) {
-                // @Bean annotation detected
-                Tbean tbean = new Tbean();
+        
+        for (Class clazz : beanClasses) {
+            // @Bean annotation detected
+            Bean bean = (Bean)clazz.getAnnotation(Bean.class);
+            Tbean tbean = new Tbean();
+            
+            // process depends-on property
+            String[] dependsOn = bean.dependsOn();
+            if (!containsValid(dependsOn)) {
+                tbean.setDependsOn(null);
+            } else {
+                List<String> dons = Arrays.asList(dependsOn);
+                tbean.setDependsOn(dons);
+            }
+            
+            // process id property
+            String id = bean.id();
+            if (id.length() > 0) {
+                tbean.setId(id);
+            } else {
+                // should we auto generate an id, based on the class name?
                 tbean.setId(clazz.getSimpleName());
-                tbean.setClazz(clazz.getName());
-                List<Object> props = tbean.getArgumentOrPropertyOrAny();
-                
-                Field[] fields = clazz.getDeclaredFields();
-                for (int i = 0; i < fields.length; i++) {
-                    if (fields[i].isAnnotationPresent(Inject.class)) {
-                       Inject inj = fields[i].getAnnotation(Inject.class);
-                       String value = inj.value();
-                       Tproperty tp = new Tproperty();
-                       tp.setName(fields[i].getName());
-                       Tvalue tvalue = new Tvalue();
-                       tvalue.setContent(value);
-                       tp.setValue(tvalue);
-                       props.add(tp);
+            }
+
+            // process the clazz property
+            tbean.setClazz(clazz.getName());
+            
+            // process activation
+            String activation = bean.activation();
+            if (activation.length() > 0) {
+                if (activation.equalsIgnoreCase("eager") || activation.equalsIgnoreCase("lazy"))
{
+                    tbean.setActivation(bean.activation());
+                } else {
+                    throw new BlueprintAnnotationException("Invalid bean activation value
" + activation + " for " + clazz.getName());
+                }
+            }
+            
+            // process description
+            if (bean.description().length() > 0) {
+                Tdescription desp = new Tdescription();
+                desp.getContent().add(bean.description());
+                tbean.setDescription(desp);
+            }
+            
+            // process scope
+            String scope = bean.scope();
+            if (scope.length() > 0) {
+                if (scope.equalsIgnoreCase("singleton") || scope.equalsIgnoreCase("prototype"))
{
+                    tbean.setScope(scope);
+                } else {
+                    throw new BlueprintAnnotationException("Invalid bean scope value " +
scope + " for " + clazz.getName());
+                }
+            }
+            
+            List<Object> props = tbean.getArgumentOrPropertyOrAny();
+
+            Field[] fields = clazz.getDeclaredFields();
+            for (int i = 0; i < fields.length; i++) {
+                if (fields[i].isAnnotationPresent(Inject.class)) {
+                    Inject inj = fields[i].getAnnotation(Inject.class);
+                    String value = inj.value();
+                    String ref = inj.ref();
+                    String name = inj.name();
+                    String desp = inj.description();
+                    
+                    
+                    Tproperty tp = new Tproperty();
+                    tp.setName(fields[i].getName());
+                    if (value.length() > 0) {
+                        Tvalue tvalue = new Tvalue();
+                        tvalue.setContent(value);
+                        tp.setValue(tvalue);
+                    }
+                    
+                    if (ref.length() > 0) {
+                        tp.setRefAttribute(ref);
                     }
+                    
+                    if (name.length() > 0) {
+                        tp.setName(name);
+                    }
+                    
+                    if (desp.length() > 0) {
+                        Tdescription tdesp = new Tdescription();
+                        tdesp.getContent().add(desp);
+                        tp.setDescription(tdesp);
+                        
+                    }
+                    props.add(tp);
                 }
+            }
+                    
+            // check if the bean also declares init and destroy annotation
+            Method[] methods = clazz.getDeclaredMethods();
+            for (int i = 0; i < methods.length; i++) {
+                if (methods[i].isAnnotationPresent(Init.class)) {
+                    tbean.setInitMethod(methods[i].getName());
+                } else if (methods[i].isAnnotationPresent(Destroy.class)) {
+                    tbean.setDestroyMethod(methods[i].getName());
+                } 
+            }
+            
+            // check if the bean also declares service
+            if (clazz.getAnnotation(Service.class) != null) {
+                Tservice tservice = generateTservice(clazz, id);
+                components.add(tservice);
+            }
+            
+            // check if the clazz implement Converter, if so, it is Converter
+            boolean isConverter = isConverter(clazz);
+            if (isConverter) {
+                TtypeConverters converters = tblueprint.getTypeConverters(); 
+                List<Object> objects = converters.getBeanOrReferenceOrRef();
+                objects.add(tbean);
+            } else {
                 components.add(tbean);
             }
         }
         
+        for (Class refClass : refClasses) {
+            Treference tref = generateTref(refClass);
+            components.add(tref);
+        }
+
+        for (Class refListClass : refListClasses) {
+            TreferenceList trefList = generateTrefList(refListClass);
+            components.add(trefList);
+        }
+
         return tblueprint;
+    }
+
+    private boolean isConverter(Class clazz) {
+        Class[] classes = clazz.getInterfaces();
+        for (int i = 0; i < classes.length; i++) {
+            if (classes[i].getName().equals(Converter.class.getName())) {
+                return true;
+            }
         
+        }
+        return false;
+
+    }
+   
+    private BigInteger convertToBigInteger(int timeout) {
+        return BigInteger.valueOf(timeout * 1000);
+    }
+
+    private boolean containsValid(String[] dependsOn) {
+        for (int i = 0; i < dependsOn.length; i++) {
+            if (dependsOn[i].length() != 0) {
+                return true;
+            }
+        }
+        return false;
+    }
 
+    // copy from blueprint extender
+    private String cachePath(Bundle bundle, String filePath) {
+        return bundle.getSymbolicName() + "/" + bundle.getVersion() + "/"
+                + filePath;
+    }
+    
+    private Treference generateTref(Class refClass) {
+        // @Reference annotation detected
+        Reference ref = (Reference)refClass.getAnnotation(Reference.class);
+        
+        String id = ref.id();
+        String availability = ref.availability();
+        String compName = ref.componentName();
+        String desp = ref.description();
+        String filter = ref.filter();
+        String pubInterface = ref.publishInterface();
+        ReferenceListener[] refListeners = ref.referenceListener();
+        int timeout = ref.timeout();
+        Treference tref = new Treference();
+        
+        // can not think of configuring depends on for reference
+        tref.setDependsOn(null);
+        
+        if (id.length() > 0) {
+            tref.setId(id);
+        }
+        
+        if (availability.length() > 0) {
+            tref.setAvailability(availability);
+        }
+        if (compName.length() > 0) {
+            tref.setComponentName(compName);
+        }
+        if (desp.length() > 0) {
+            Tdescription value = new Tdescription();
+            value.getContent().add(desp);
+            tref.setDescription(value);
+        }
+        if (filter.length() > 0) {
+            tref.setFilter(filter);
+        }
+        if (pubInterface.length() > 0) {
+            tref.setInterface(pubInterface);
+        } else {
+            boolean isInterface =  refClass.isInterface();
+            if (isInterface) {
+                tref.setInterface(refClass.getName());
+            } else {
+                // should we throw an exception?  
+            }
+        }
+        
+        if (timeout > 0) {
+            tref.setTimeout(convertToBigInteger(timeout));
+        }
+        for (ReferenceListener rl : refListeners) {
+            TreferenceListener trl = new TreferenceListener();
+            String rf = rl.ref();
+            String bindMethod = rl.bind();
+            String unbindMethod = rl.unbind();
+            trl.setRefAttribute(rf);
+            trl.setBindMethod(bindMethod);
+            trl.setUnbindMethod(unbindMethod);
+            tref.getReferenceListener().add(trl);
+        }
+        
+        return tref;
     }
     
+    private TreferenceList generateTrefList(Class refClass) {
+        // @ReferenceList annotation detected
+        ReferenceList ref = (ReferenceList)refClass.getAnnotation(ReferenceList.class);
+        
+        String id = ref.id();
+        String availability = ref.availability();
+        String compName = ref.componentName();
+        String desp = ref.description();
+        String filter = ref.filter();
+        String pubInterface = ref.publishInterface();
+        ReferenceListener[] refListeners = ref.referenceListener();
+        TreferenceList tref = new TreferenceList();
+        
+        // can not think of configuring depends on for referencelist
+        tref.setDependsOn(null);
+        
+        if (id.length() > 0) {
+            tref.setId(id);
+        }
+        
+        if (availability.length() > 0) {
+            tref.setAvailability(availability);
+        }
+        if (compName.length() > 0) {
+            tref.setComponentName(compName);
+        }
+        if (desp.length() > 0) {
+            Tdescription value = new Tdescription();
+            value.getContent().add(desp);
+            tref.setDescription(value);
+        }
+        if (filter.length() > 0) {
+            tref.setFilter(filter);
+        }
+        if (pubInterface.length() > 0) {
+            tref.setInterface(pubInterface);
+        } else {
+            boolean isInterface =  refClass.isInterface();
+            if (isInterface) {
+                tref.setInterface(refClass.getName());
+            } else {
+                // should we throw an exception?  
+            }
+        }
+        for (ReferenceListener rl : refListeners) {
+            TreferenceListener trl = new TreferenceListener();
+            String rf = rl.ref();
+            String bindMethod = rl.bind();
+            String unbindMethod = rl.unbind();
+            trl.setRefAttribute(rf);
+            trl.setBindMethod(bindMethod);
+            trl.setUnbindMethod(unbindMethod);
+            tref.getReferenceListener().add(trl);
+        }
+        
+        return tref;
+    }
     
-    // copy from blueprint extender
-    private String cachePath(Bundle bundle, String filePath)
-    {
-      return bundle.getSymbolicName() + "/" + bundle.getVersion() + "/" + filePath;
-    } 
+    private Tservice generateTservice(Class clazz, String id) {
+        Service service = (Service) clazz.getAnnotation(Service.class);
+        String pInterface = service.publishInterface();
+        int ranking = service.ranking();
+        String autoExport = service.autoExport();
+        RegistrationListener[] regListeners = service.registerationListener();
+        
+        Tservice tservice = new Tservice();
+        
+        // can not think of configuring depends on for service
+        tservice.setDependsOn(null);
+        
+        // use the bean id as the ref value since we are exposing service for the bean
+        tservice.setRefAttribute(id);
+        
+        if (autoExport.length() > 0) {
+            tservice.setAutoExport(autoExport);
+        }
+        if (ranking > 0) {
+            tservice.setRanking(ranking);
+        }
+        if (pInterface.length() > 0) {
+            // TODO add support for multiple interfaces
+            tservice.setInterface(pInterface);
+        }
+        
+        for (RegistrationListener regListener : regListeners) {
+            String regListenerId = regListener.id();
+            if (regListenerId.length() > 0) {
+                TregistrationListener tregListener = new TregistrationListener();
+                tregListener.setRefAttribute(regListenerId);
+                tregListener.setRegistrationMethod(regListener.register());
+                tregListener.setUnregistrationMethod(regListener.unregister());
+                tservice.getRegistrationListener().add(tregListener);
+                
+            } else {
+                throw new BlueprintAnnotationException("No ref id for service registration
listener " + " for " + clazz.getName());
+            }
+        }
+        
+        return tservice;
+    }
 }

Added: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java?rev=941492&view=auto
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
(added)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
Wed May  5 21:12:31 2010
@@ -0,0 +1,72 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.aries.blueprint.annotation;
+
+import java.io.File;
+import java.util.List;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+
+import org.apache.aries.blueprint.jaxb.Tbean;
+import org.apache.aries.blueprint.jaxb.Tblueprint;
+import org.apache.aries.blueprint.jaxb.Tproperty;
+import org.apache.aries.blueprint.jaxb.Tvalue;
+
+import junit.framework.TestCase;
+
+public class AnnotationTest extends TestCase {
+
+    public void testGenerationFile() throws Exception {
+        File file = new File("test-annotation.xml");
+        if (file.exists()) {
+            file.delete();
+            file.createNewFile();
+        }
+
+        Tblueprint tblueprint = new Tblueprint();
+        List<Object> components = tblueprint.getServiceOrReferenceListOrBean();
+        Tbean tbean = new Tbean();
+        tbean.setDependsOn(null);
+        tbean.setId("Bar");
+        tbean.setClazz("org.apache.aries.blueprint.sample.Bar");
+        List<Object> props = tbean.getArgumentOrPropertyOrAny();
+
+        String value = "Hello Bar";
+        Tproperty tp = new Tproperty();
+        tp.setName("value");
+        //Tvalue tvalue = new Tvalue();
+        //tvalue.setContent(value);
+        //tp.setValue(tvalue);
+        tp.setValueAttribute(value);
+        props.add(tp);
+
+        components.add(tbean);
+
+        marshallOBRModel(tblueprint, file);
+    }
+
+    private void marshallOBRModel(Tblueprint tblueprint, File blueprintFile)
+            throws JAXBException {
+        JAXBContext context = JAXBContext.newInstance(Tblueprint.class);
+        Marshaller marshaller = context.createMarshaller();
+        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+        marshaller.marshal(tblueprint, blueprintFile);
+
+    }
+}

Propchange: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message