felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r638018 - in /felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo: ./ handlers/configuration/ handlers/dependency/ handlers/lifecycle/controller/ handlers/providedservice/ util/
Date Mon, 17 Mar 2008 18:40:48 GMT
Author: clement
Date: Mon Mar 17 11:40:47 2008
New Revision: 638018

URL: http://svn.apache.org/viewvc?rev=638018&view=rev
Log:
Modify injection mechanism to avoid useless methods call. Now, handlers can register others
objects to be notified of field and method accesses. They do no more need to delegate calls,
the registered interceptors receive directly the notifications.

Added:
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/FieldInterceptor.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/MethodInterceptor.java
Modified:
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/controller/ControllerHandler.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java
    felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Property.java

Added: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/FieldInterceptor.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/FieldInterceptor.java?rev=638018&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/FieldInterceptor.java
(added)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/FieldInterceptor.java
Mon Mar 17 11:40:47 2008
@@ -0,0 +1,47 @@
+/* 
+ * 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.felix.ipojo;
+
+/**
+* Field interceptor.
+* A class implementing this interface is able to be notified of field accesses.
+* The listener need to be register on the instance manager. 
+* 
+* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+*/
+public interface FieldInterceptor {
+    
+    /**
+     * This method is called when a PUTFIELD operation is detected.
+     * @param pojo : the pojo object setting the value
+     * @param fieldName : the field name
+     * @param value : the value passed to the field
+     */
+    void onSet(Object pojo, String fieldName, Object value);
+
+    /**
+     * This method is called when a GETFIELD operation is detected.
+     * @param pojo : the pojo object getting the value
+     * @param fieldName : the field name
+     * @param value : the value passed to the field (by the previous call)
+     * @return : the managed value of the field
+     */
+    Object onGet(Object pojo, String fieldName, Object value);
+
+}

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
Mon Mar 17 11:40:47 2008
@@ -77,12 +77,12 @@
     private BundleContext m_context;
 
     /**
-     * Map [field, handler list] storing handlers interested by the field.
+     * Map [field, field interceptor list] storing handlers interested by the field.
      */
     private Map m_fieldRegistration;
 
     /**
-     * Map [method identifier, handler list] storing handlers interested by the method.
+     * Map [method identifier, method interceptor list] storing handlers interested by the
method.
      */
     private Map m_methodRegistration;
 
@@ -426,7 +426,7 @@
      * @param listener : listener to register.
      * @see org.apache.felix.ipojo.ComponentInstance#addInstanceStateListener(org.apache.felix.ipojo.InstanceStateListener)
      */
-    public void addInstanceStateListener(InstanceStateListener listener) {
+    public synchronized void addInstanceStateListener(InstanceStateListener listener) {
         if (m_listeners == null) {
             m_listeners = new ArrayList();
         }
@@ -440,7 +440,7 @@
      * @param listener : listener to unregister.
      * @see org.apache.felix.ipojo.ComponentInstance#removeInstanceStateListener(org.apache.felix.ipojo.InstanceStateListener)
      */
-    public void removeInstanceStateListener(InstanceStateListener listener) {
+    public synchronized void removeInstanceStateListener(InstanceStateListener listener)
{
         if (m_listeners != null) {
             synchronized (m_listeners) {
                 m_listeners.remove(listener);
@@ -502,8 +502,8 @@
                 try {
                     Constructor cst = m_clazz.getDeclaredConstructor(new Class[] { InstanceManager.class,
BundleContext.class });
                     cst.setAccessible(true);
-                    Object[] args = new Object[] { this, m_context }; //TODO hide the instance
manager.
-                    onEntry(null, m_className,  args);
+                    Object[] args = new Object[] { this, m_context };
+                    onEntry(null, m_className,  new Object[] {m_context});
                     instance = cst.newInstance(args);
                     onExit(null, m_className, instance);
                 } catch (NoSuchMethodException e) {
@@ -511,8 +511,8 @@
                     if (instance == null) {
                         Constructor cst = m_clazz.getDeclaredConstructor(new Class[] { InstanceManager.class
});
                         cst.setAccessible(true);
-                        Object[] args = new Object[] { this }; // TODO hide the instance
manager.
-                        onEntry(null, m_className, args);
+                        Object[] args = new Object[] {this};
+                        onEntry(null, m_className, new Object[0]);
                         instance = cst.newInstance(args);
                         onExit(null, m_className, instance);
                     }
@@ -662,51 +662,70 @@
      * @param handler : the handler to register
      * @param fields : the field metadata list
      * @param methods : the method metadata list
+     * @deprecated use register(FieldMetadata fm, FieldInterceptor fi) and register(MethodMetadata
mm, MethodInterceptor mi) instead. 
      */
     public void register(PrimitiveHandler handler, FieldMetadata[] fields, MethodMetadata[]
methods) {
         for (int i = 0; fields != null && i < fields.length; i++) {
-            if (m_fieldRegistration == null) {
-                m_fieldRegistration = new HashMap();
-                m_fieldRegistration.put(fields[i].getFieldName(), new PrimitiveHandler[]
{ handler });
+            register(fields[i], handler);
+        }
+        for (int i = 0; methods != null && i < methods.length; i++) {
+            register(methods[i], handler);
+        }
+
+    }
+    
+    /**
+     * Register a field interceptor.
+     * @param field : intercepted field
+     * @param interceptor : interceptor
+     */
+    public void register(FieldMetadata field, FieldInterceptor interceptor) {
+        if (m_fieldRegistration == null) {
+            m_fieldRegistration = new HashMap();
+            m_fieldRegistration.put(field.getFieldName(), new FieldInterceptor[] { interceptor
});
+        } else {
+            FieldInterceptor[] list = (FieldInterceptor[]) m_fieldRegistration.get(field.getFieldName());
+            if (list == null) {
+                m_fieldRegistration.put(field.getFieldName(), new FieldInterceptor[] { interceptor
});
             } else {
-                PrimitiveHandler[] list = (PrimitiveHandler[]) m_fieldRegistration.get(fields[i].getFieldName());
-                if (list == null) {
-                    m_fieldRegistration.put(fields[i].getFieldName(), new PrimitiveHandler[]
{ handler });
-                } else {
-                    for (int j = 0; j < list.length; j++) {
-                        if (list[j] == handler) {
-                            return;
-                        }
+                for (int j = 0; j < list.length; j++) {
+                    if (list[j] == interceptor) {
+                        return;
                     }
-                    PrimitiveHandler[] newList = new PrimitiveHandler[list.length + 1];
-                    System.arraycopy(list, 0, newList, 0, list.length);
-                    newList[list.length] = handler;
-                    m_fieldRegistration.put(fields[i].getFieldName(), newList);
                 }
+                FieldInterceptor[] newList = new FieldInterceptor[list.length + 1];
+                System.arraycopy(list, 0, newList, 0, list.length);
+                newList[list.length] = interceptor;
+                m_fieldRegistration.put(field.getFieldName(), newList);
             }
         }
-        for (int i = 0; methods != null && i < methods.length; i++) {
-            if (m_methodRegistration == null) {
-                m_methodRegistration = new HashMap();
-                m_methodRegistration.put(methods[i].getMethodIdentifier(), new PrimitiveHandler[]
{ handler });
+    }
+    
+    /**
+     * Register a method interceptor.
+     * @param method : intercepted method
+     * @param interceptor : interceptor
+     */
+    public void register(MethodMetadata method, MethodInterceptor interceptor) {
+        if (m_methodRegistration == null) {
+            m_methodRegistration = new HashMap();
+            m_methodRegistration.put(method.getMethodIdentifier(), new MethodInterceptor[]
{ interceptor });
+        } else {
+            MethodInterceptor[] list = (MethodInterceptor[]) m_methodRegistration.get(method.getMethodIdentifier());
+            if (list == null) {
+                m_methodRegistration.put(method.getMethodIdentifier(), new MethodInterceptor[]
{ interceptor });
             } else {
-                PrimitiveHandler[] list = (PrimitiveHandler[]) m_methodRegistration.get(methods[i].getMethodIdentifier());
-                if (list == null) {
-                    m_methodRegistration.put(methods[i].getMethodIdentifier(), new PrimitiveHandler[]
{ handler });
-                } else {
-                    for (int j = 0; j < list.length; j++) {
-                        if (list[j] == handler) {
-                            return;
-                        }
+                for (int j = 0; j < list.length; j++) {
+                    if (list[j] == interceptor) {
+                        return;
                     }
-                    PrimitiveHandler[] newList = new PrimitiveHandler[list.length + 1];
-                    System.arraycopy(list, 0, newList, 0, list.length);
-                    newList[list.length] = handler;
-                    m_methodRegistration.put(methods[i].getMethodIdentifier(), newList);
                 }
+                MethodInterceptor[] newList = new MethodInterceptor[list.length + 1];
+                System.arraycopy(list, 0, newList, 0, list.length);
+                newList[list.length] = interceptor;
+                m_methodRegistration.put(method.getMethodIdentifier(), newList);
             }
         }
-
     }
 
     /**
@@ -721,7 +740,7 @@
         Object result = initialValue;
         // Get the list of registered handlers
 
-        PrimitiveHandler[] list = (PrimitiveHandler[]) m_fieldRegistration.get(fieldName);
+        FieldInterceptor[] list = (FieldInterceptor[]) m_fieldRegistration.get(fieldName);
         for (int i = 0; list != null && i < list.length; i++) {
             Object handlerResult = list[i].onGet(null, fieldName, initialValue);
             if (handlerResult == initialValue) {
@@ -732,9 +751,7 @@
                         m_factory.getLogger().log(
                                                   Logger.WARNING,
                                                   "A conflict was detected on the injection
of "
-                                                          + fieldName
-                                                          + " - return the last value from
"
-                                                          + list[i].getHandlerManager().getInstanceName());
+                                                          + fieldName);
                     }
                 }
                 result = handlerResult;
@@ -762,7 +779,7 @@
         if (m_methodRegistration == null) {
             return;
         }
-        PrimitiveHandler[] list = (PrimitiveHandler[]) m_methodRegistration.get(methodId);
+        MethodInterceptor[] list = (MethodInterceptor[]) m_methodRegistration.get(methodId);
         Method method = getMethodById(methodId);
         for (int i = 0; list != null && i < list.length; i++) {
             list[i].onEntry(pojo, method, args);
@@ -780,7 +797,7 @@
         if (m_methodRegistration == null) {
             return;
         }
-        PrimitiveHandler[] list = (PrimitiveHandler[]) m_methodRegistration.get(methodId);
+        MethodInterceptor[] list = (MethodInterceptor[]) m_methodRegistration.get(methodId);
         Method method = getMethodById(methodId);
         for (int i = 0; list != null && i < list.length; i++) {
             list[i].onExit(pojo, method, result);
@@ -801,7 +818,7 @@
         if (m_methodRegistration == null) {
             return;
         }
-        PrimitiveHandler[] list = (PrimitiveHandler[]) m_methodRegistration.get(methodId);
+        MethodInterceptor[] list = (MethodInterceptor[]) m_methodRegistration.get(methodId);
         Method method = getMethodById(methodId);
         for (int i = 0; list != null && i < list.length; i++) {
             list[i].onError(pojo, method, error);
@@ -851,7 +868,7 @@
         Object value = m_fields.get(fieldName);
         if ((value != null && !value.equals(objectValue)) || (value == null &&
objectValue != null)) {
             m_fields.put(fieldName, objectValue);
-            PrimitiveHandler[] list = (PrimitiveHandler[]) m_fieldRegistration.get(fieldName);
+            FieldInterceptor[] list = (FieldInterceptor[]) m_fieldRegistration.get(fieldName);
             for (int i = 0; list != null && i < list.length; i++) {
                 list[i].onSet(null, fieldName, objectValue);
             }

Added: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/MethodInterceptor.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/MethodInterceptor.java?rev=638018&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/MethodInterceptor.java
(added)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/MethodInterceptor.java
Mon Mar 17 11:40:47 2008
@@ -0,0 +1,69 @@
+/* 
+ * 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.felix.ipojo;
+
+import java.lang.reflect.Method;
+
+/**
+* Method interceptor.
+* A class implementing this interface is able to be notified of method invocation.
+* The listener need to be register on the instance manager. 
+* For event are send to the listener : before the method entry, after the method returns,

+* when an error is thrown by the method, and before the after either a returns or an error
(finally) 
+* 
+* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+*/
+public interface MethodInterceptor {
+    
+    /**
+     * This method is called when the execution enter in a method.
+     * @param pojo : pojo on which the method is called.
+     * @param method : method invoked.
+     * @param args arguments array.
+     */
+    void onEntry(Object pojo, Method method, Object[] args);
+
+    /**
+     * This method is called when the execution exit a method (before a return or a throw).
+     * If the given returned object is null, either the method is void, either it returns
null.
+     * You must not modified the returned object.
+     * @param pojo : the pojo on which the method exits.
+     * @param method : exiting method.
+     * @param returnedObj : the returned object (boxed for primitive type)
+     */
+    void onExit(Object pojo, Method method, Object returnedObj);
+    
+    /**
+     * This method is called when the execution throw an exception in the given method.
+     * @param pojo : the pojo on which the method was accessed.
+     * @param method : invoked method.
+     * @param throwable : the thrown exception
+     */
+    void onError(Object pojo, Method method, Throwable throwable);
+    
+    /**
+     * This method is called when the execution of a method will terminate : 
+     * just before to throw an exception or before to return.
+     * OnError or OnExit was already called.
+     * @param pojo : the pojo on which the method was accessed.
+     * @param method : invoked method.
+     */
+    void onFinally(Object pojo, Method method);
+
+}

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/PrimitiveHandler.java
Mon Mar 17 11:40:47 2008
@@ -31,7 +31,7 @@
 * 
 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
 */
-public abstract class PrimitiveHandler extends Handler {
+public abstract class PrimitiveHandler extends Handler implements FieldInterceptor, MethodInterceptor
{
     
     /**
      * "Primitive" Handler type (value).

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
Mon Mar 17 11:40:47 2008
@@ -18,10 +18,8 @@
  */
 package org.apache.felix.ipojo.handlers.configuration;
 
-import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.Enumeration;
-import java.util.List;
 import java.util.Properties;
 
 import org.apache.felix.ipojo.ConfigurationException;
@@ -169,8 +167,6 @@
             m_toPropagate = configuration;
         }
 
-        List fields = new ArrayList();
-
         for (int i = 0; configurables != null && i < configurables.length; i++)
{
             String fieldName = configurables[i].getAttribute("field");
             String methodName = configurables[i].getAttribute("method");
@@ -180,11 +176,6 @@
 
             String type = configurables[i].getAttribute("type"); // The initialize method
has fixed the property name.
             
-            if (fieldName != null) {
-                FieldMetadata field = new FieldMetadata(fieldName, type);
-                fields.add(field);
-            }
-            
             Property prop = new Property(name, fieldName, methodName, value, type, getInstanceManager(),
this);
             addProperty(prop);
 
@@ -196,8 +187,12 @@
             } else {
                 prop.setValue(configuration.get(name));
             }
+            
+            if (fieldName != null) {
+                FieldMetadata field = new FieldMetadata(fieldName, type);
+                getInstanceManager().register(field, prop);
+            }
         }
-        getInstanceManager().register(this, (FieldMetadata[]) fields.toArray(new FieldMetadata[fields.size()]),
null);
     }
 
     /**
@@ -227,47 +222,47 @@
         }
     }
 
-    /**
-     * Setter Callback Method.
-     * Check if the modified field is a configurable property to update the value.
-     * @param pojo : the pojo object on which the field is accessed
-     * @param fieldName : field name
-     * @param value : new value
-     * @see org.apache.felix.ipojo.Handler#onSet(Object, java.lang.String, java.lang.Object)
-     */
-    public void onSet(Object pojo, String fieldName, Object value) {
-        // Verify that the field name correspond to a configurable property
-        for (int i = 0; i < m_configurableProperties.length; i++) {
-            Property prop = m_configurableProperties[i];
-            if (prop.hasField() && prop.getField().equals(fieldName)) {
-                // Check if the value has changed
-                if (prop.getValue() == null || !prop.getValue().equals(value)) {
-                    prop.setValue(value); // Change the value
-                }
-            }
-        }
-        // Else do nothing
-    }
-
-    /**
-     * Getter Callback Method.
-     * Check if the field is a configurable property to push the stored value.
-     * @param pojo : the pojo object on which the field is accessed
-     * @param fieldName : field name
-     * @param value : value pushed by the previous handler
-     * @return the stored value or the previous value.
-     * @see org.apache.felix.ipojo.Handler#onGet(Object,
-     * java.lang.String, java.lang.Object)
-     */
-    public Object onGet(Object pojo, String fieldName, Object value) {
-        // Check if the field is a configurable property
-        for (int i = 0; i < m_configurableProperties.length; i++) {
-            if (fieldName.equals(m_configurableProperties[i].getField())) { 
-                return m_configurableProperties[i].getValue(); 
-            }
-        }
-        return value;
-    }
+//    /**
+//     * Setter Callback Method.
+//     * Check if the modified field is a configurable property to update the value.
+//     * @param pojo : the pojo object on which the field is accessed
+//     * @param fieldName : field name
+//     * @param value : new value
+//     * @see org.apache.felix.ipojo.Handler#onSet(Object, java.lang.String, java.lang.Object)
+//     */
+//    public void onSet(Object pojo, String fieldName, Object value) {
+//        // Verify that the field name correspond to a configurable property
+//        for (int i = 0; i < m_configurableProperties.length; i++) {
+//            Property prop = m_configurableProperties[i];
+//            if (prop.hasField() && prop.getField().equals(fieldName)) {
+//                // Check if the value has changed
+//                if (prop.getValue() == null || !prop.getValue().equals(value)) {
+//                    prop.setValue(value); // Change the value
+//                }
+//            }
+//        }
+//        // Else do nothing
+//    }
+//
+//    /**
+//     * Getter Callback Method.
+//     * Check if the field is a configurable property to push the stored value.
+//     * @param pojo : the pojo object on which the field is accessed
+//     * @param fieldName : field name
+//     * @param value : value pushed by the previous handler
+//     * @return the stored value or the previous value.
+//     * @see org.apache.felix.ipojo.Handler#onGet(Object,
+//     * java.lang.String, java.lang.Object)
+//     */
+//    public Object onGet(Object pojo, String fieldName, Object value) {
+//        // Check if the field is a configurable property
+//        for (int i = 0; i < m_configurableProperties.length; i++) {
+//            if (fieldName.equals(m_configurableProperties[i].getField())) { 
+//                return m_configurableProperties[i].getValue(); 
+//            }
+//        }
+//        return value;
+//    }
 
     /**
      * Handler state changed.

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
Mon Mar 17 11:40:47 2008
@@ -20,12 +20,15 @@
 
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
 
+import org.apache.felix.ipojo.FieldInterceptor;
 import org.apache.felix.ipojo.InstanceManager;
+import org.apache.felix.ipojo.MethodInterceptor;
 import org.apache.felix.ipojo.Nullable;
 import org.apache.felix.ipojo.PolicyServiceContext;
 import org.apache.felix.ipojo.handlers.dependency.ServiceUsage.Usage;
@@ -38,7 +41,7 @@
  * Represent a service dependency of the component instance.
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
-public class Dependency extends DependencyModel {
+public class Dependency extends DependencyModel implements FieldInterceptor, MethodInterceptor
{
 
     /**
      * Reference on the Dependency Handler.
@@ -177,56 +180,6 @@
     public DependencyHandler getHandler() {
         return m_handler;
     }
-    
-
-
-    /**
-     * This method is called by the replaced code in the component
-     * implementation class. Construct the service object list is necessary.
-     * 
-     * @return null or a service object or a list of service object according to
-     * the dependency.
-     */
-    Object get() {
-        // Initialize the thread local object is not already touched.
-        Usage usage = (Usage) m_usage.get();
-        if (usage.m_stack == 0) { // uninitialized usage.
-            ServiceReference[] refs = super.getServiceReferences();
-            if (isAggregate()) { // If we are aggregate we get the objects.
-                if (refs == null) {
-                    usage.m_objects = (Object[]) Array.newInstance(getSpecification(), 0);
// Create an empty array.
-                } else {
-                   // Use a reflective construction to avoid class cast exception. This method
allow to set the component type.
-                    usage.m_objects = (Object[]) Array.newInstance(getSpecification(), refs.length);

-                    for (int i = 0; refs != null && i < refs.length; i++) {
-                        ServiceReference ref = refs[i];
-                        usage.m_objects[i] = getService(ref);
-                    }
-                }
-            } else { // We are singular.
-                // Use a reflective construction to avoid class cast exception. This method
allow to set the component type.
-                usage.m_objects = (Object[]) Array.newInstance(getSpecification(), 1);
-                if (refs == null) {
-                    if (m_nullable == null) {
-                        m_handler.warn("[" + m_handler.getInstanceManager().getInstanceName()
+ "] The dependency is not optional, however no service object can be injected in " + m_field
+ " -> " + getSpecification().getName());
-                        return null;
-                    }
-                    usage.m_objects[0] = m_nullable;
-                } else {
-                    ServiceReference ref = getServiceReference();
-                    usage.m_objects[0] = getService(ref);
-                }
-            }
-            usage.inc(); // Start the tracking, so fix the stack level to 1
-            m_usage.set(usage);
-        }
-
-        if (isAggregate()) { // Multiple dependency
-            return usage.m_objects;
-        } else {
-            return usage.m_objects[0];
-        }
-    }
 
     public synchronized boolean isFrozen() {
         return m_isFrozen;
@@ -299,7 +252,7 @@
             m_handler.error("The method " + callback.getMethodName() + " is not accessible
in the class " + m_handler.getInstanceManager().getClassName());
             m_handler.getInstanceManager().stop();
         } catch (InvocationTargetException e) {
-            m_handler.error("The method " + callback.getMethodName() + " in the class " +
m_handler.getInstanceManager().getClassName() + "throws an exception : " + e.getTargetException().getMessage());
+            m_handler.error("The method " + callback.getMethodName() + " in the class " +
m_handler.getInstanceManager().getClassName() + " throws an exception : " + e.getTargetException().getMessage(),
e.getTargetException());
             m_handler.getInstanceManager().stop();
         }
 
@@ -373,46 +326,6 @@
         return m_isServiceLevelRequirement;
     }
 
-    /**
-     * Method called when a thread enters in a method.
-     */
-    public void entry() {
-        if (m_usage == null) {
-            //DEBUG
-            if (m_field != null) {
-                System.out.println("Eror in dependency : field " + m_field + " but no usage");
-            }
-            return;
-        }
-        
-        Usage usage = (Usage) m_usage.get();
-        if (usage.m_stack > 0) {
-            usage.inc();
-            m_usage.set(usage); // Set the Thread local as value has been modified 
-        }
-    }
-
-    /**
-     * Method called when a thread exits a method.
-     */
-    public void exit() {
-        if (m_usage == null) {
-            //DEBUG
-            if (m_field != null) {
-                System.out.println("Eror in dependency : field " + m_field + " but no usage");
-            }
-            return;
-        }
-        
-        Usage usage = (Usage) m_usage.get();
-        if (usage.m_stack > 0) {
-            if (usage.dec()) {
-                // Exit the method flow => Release all objects
-                usage.clear();
-                m_usage.set(usage); // Set the Thread local as value has been modified
-            }
-        }
-    }
 
     /**
      * A new service has to be injected.
@@ -453,6 +366,128 @@
             return null;
         } else {
             return Arrays.asList(refs);
+        }
+    }
+
+    /**
+     * This method is called by the replaced code in the component
+     * implementation class. Construct the service object list is necessary.
+     * @param pojo : POJO object.
+     * @param fieldName : field
+     * @param value : last value.
+     * @return the service object or a nullable / default implementation if defined.
+     * @see org.apache.felix.ipojo.FieldInterceptor#onGet(java.lang.Object, java.lang.String,
java.lang.Object)
+     */
+    public Object onGet(Object pojo, String fieldName, Object value) {
+     // Initialize the thread local object is not already touched.
+        Usage usage = (Usage) m_usage.get();
+        if (usage.m_stack == 0) { // uninitialized usage.
+            ServiceReference[] refs = super.getServiceReferences();
+            if (isAggregate()) { // If we are aggregate we get the objects.
+                if (refs == null) {
+                    usage.m_objects = (Object[]) Array.newInstance(getSpecification(), 0);
// Create an empty array.
+                } else {
+                   // Use a reflective construction to avoid class cast exception. This method
allow to set the component type.
+                    usage.m_objects = (Object[]) Array.newInstance(getSpecification(), refs.length);

+                    for (int i = 0; refs != null && i < refs.length; i++) {
+                        ServiceReference ref = refs[i];
+                        usage.m_objects[i] = getService(ref);
+                    }
+                }
+            } else { // We are singular.
+                // Use a reflective construction to avoid class cast exception. This method
allow to set the component type.
+                usage.m_objects = (Object[]) Array.newInstance(getSpecification(), 1);
+                if (refs == null) {
+                    if (m_nullable == null) {
+                        m_handler.warn("[" + m_handler.getInstanceManager().getInstanceName()
+ "] The dependency is not optional, however no service object can be injected in " + m_field
+ " -> " + getSpecification().getName());
+                        return null;
+                    }
+                    usage.m_objects[0] = m_nullable;
+                } else {
+                    ServiceReference ref = getServiceReference();
+                    usage.m_objects[0] = getService(ref);
+                }
+            }
+            usage.inc(); // Start the tracking, so fix the stack level to 1
+            m_usage.set(usage);
+        }
+
+        if (isAggregate()) { // Multiple dependency
+            return usage.m_objects;
+        } else {
+            return usage.m_objects[0];
+        }
+    }
+
+    /**
+     * The field was set.
+     * This method should not be call if the POJO is written correctly.
+     * @param pojo : POJO object
+     * @param fieldName : field name
+     * @param value : set value.
+     * @see org.apache.felix.ipojo.FieldInterceptor#onSet(java.lang.Object, java.lang.String,
java.lang.Object)
+     */
+    public void onSet(Object pojo, String fieldName, Object value) {
+        // Nothing to do.
+    }
+
+    /**
+     * A POJO method will be invoked.
+     * @param pojo : Pojo object
+     * @param method : called method
+     * @param args : arguments
+     * @see org.apache.felix.ipojo.MethodInterceptor#onEntry(java.lang.Object, java.lang.reflect.Method,
java.lang.Object[])
+     */
+    public void onEntry(Object pojo, Method method, Object[] args) {
+        if (m_usage != null) {
+            Usage usage = (Usage) m_usage.get();
+            if (usage.m_stack > 0) {
+                usage.inc();
+                m_usage.set(usage); // Set the Thread local as value has been modified
+            }
+        }
+    }
+
+    /**
+     * A POJO method has thrown an error.
+     * This method does nothing and wait for the finally.
+     * @param pojo : POJO object.
+     * @param method : Method object.
+     * @param throwable : thrown error
+     * @see org.apache.felix.ipojo.MethodInterceptor#onError(java.lang.Object, java.lang.reflect.Method,
java.lang.Throwable)
+     */
+    public void onError(Object pojo, Method method, Throwable throwable) {
+        // Nothing to do  : wait onFinally
+    }
+
+    /**
+     * A POJO method has returned.
+     * @param pojo : POJO object.
+     * @param method : Method object.
+     * @param returnedObj : returned object (null for void method)
+     * @see org.apache.felix.ipojo.MethodInterceptor#onExit(java.lang.Object, java.lang.reflect.Method,
java.lang.Object)
+     */
+    public void onExit(Object pojo, Method method, Object returnedObj) {
+        // Nothing to do  : wait onFinally
+        
+    }
+
+    /**
+     * A POJO method is finished.
+     * @param pojo : POJO object.
+     * @param method : Method object.
+     * @see org.apache.felix.ipojo.MethodInterceptor#onFinally(java.lang.Object, java.lang.reflect.Method)
+     */
+    public void onFinally(Object pojo, Method method) {
+        if (m_usage != null) {
+            Usage usage = (Usage) m_usage.get();
+            if (usage.m_stack > 0) {
+                if (usage.dec()) {
+                    // Exit the method flow => Release all objects
+                    usage.clear();
+                    m_usage.set(usage); // Set the Thread local as value has been modified
+                }
+            }
         }
     }
 

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
Mon Mar 17 11:40:47 2008
@@ -18,11 +18,8 @@
  */
 package org.apache.felix.ipojo.handlers.dependency;
 
-import java.lang.reflect.Method;
-import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.Dictionary;
-import java.util.List;
 
 import org.apache.felix.ipojo.ConfigurationException;
 import org.apache.felix.ipojo.IPojoContext;
@@ -263,7 +260,7 @@
     public void configure(Element componentMetadata, Dictionary configuration) throws ConfigurationException
{
         // getPojoMetadata();
         PojoMetadata manipulation = getFactory().getPojoMetadata();
-        List fields = new ArrayList();
+        boolean atLeastOneField = false;
 
         // Create the dependency according to the component metadata
         Element[] deps = componentMetadata.getElements("Requires");
@@ -349,60 +346,41 @@
             if (checkDependency(dep, manipulation)) {
                 addDependency(dep);
                 if (dep.getField() != null) {
-                    fields.add(manipulation.getField(dep.getField()));
+                    getInstanceManager().register(manipulation.getField(dep.getField()),
dep);
+                    atLeastOneField = true;
                 }
             }
         }
 
-        if (! fields.isEmpty()) { // Does register only if we have fields
-            getInstanceManager().register(this, (FieldMetadata[]) fields.toArray(new FieldMetadata[fields.size()]),
manipulation.getMethods());
-        }
-    }
-
-    /**
-     * GetterCallback Method.
-     * @param pojo : the pojo object on which the field is accessed
-     * @param fieldName : the field name.
-     * @param value : the value passed to the field (by the previous handler).
-     * @return the object that the dependency handler want to push.
-     * @see org.apache.felix.ipojo.Handler#onGet(Object, java.lang.String, java.lang.Object)
-     */
-    public Object onGet(Object pojo, String fieldName, Object value) {
-        for (int i = 0; i < m_dependencies.length; i++) {
-            Dependency dep = m_dependencies[i];
-            if (fieldName.equals(dep.getField())) {
-                // The field name is a dependency, return the get
-                return dep.get();
+        if (atLeastOneField) { // Does register only if we have fields
+            MethodMetadata[] methods = manipulation.getMethods();
+            for (int i = 0; i < methods.length; i++) {
+                for (int j = 0; j < m_dependencies.length; j++) {
+                    getInstanceManager().register(methods[i], m_dependencies[j]);
+                }
             }
         }
-        // Else return the value
-        return value;
     }
 
-    /**
-     * Method Entry callback.
-     * @param pojo : pojo object on which the method is invoked.
-     * @param method : method object representing the invoked method.
-     * @param args : argument array
-     * @see org.apache.felix.ipojo.Handler#onEntry(Object, Method, Object[])
-     */
-    public void onEntry(Object pojo, Method method, Object[] args) {
-        for (int i = 0; i < m_dependencies.length; i++) {
-            m_dependencies[i].entry();
-        }
-    }
-
-    /**
-     * Method onFinally implementation.
-     * @param pojo : pojo object on which the method was invoked.
-     * @param method : method object representing the invoked method.
-     * @see org.apache.felix.ipojo.PrimitiveHandler#onFinally(java.lang.Object, java.lang.reflect.Method)
-     */
-    public void onFinally(Object pojo, Method method) {
-        for (int i = 0; i < m_dependencies.length; i++) {
-            m_dependencies[i].exit();
-        }
-    }
+//    /**
+//     * GetterCallback Method.
+//     * @param pojo : the pojo object on which the field is accessed
+//     * @param fieldName : the field name.
+//     * @param value : the value passed to the field (by the previous handler).
+//     * @return the object that the dependency handler want to push.
+//     * @see org.apache.felix.ipojo.Handler#onGet(Object, java.lang.String, java.lang.Object)
+//     */
+//    public Object onGet(Object pojo, String fieldName, Object value) {
+//        for (int i = 0; i < m_dependencies.length; i++) {
+//            Dependency dep = m_dependencies[i];
+//            if (fieldName.equals(dep.getField())) {
+//                // The field name is a dependency, return the get
+//                return dep.get();
+//            }
+//        }
+//        // Else return the value
+//        return value;
+//    }
 
     /**
      * Handler start method.

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/controller/ControllerHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/controller/ControllerHandler.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/controller/ControllerHandler.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/controller/ControllerHandler.java
Mon Mar 17 11:40:47 2008
@@ -52,7 +52,7 @@
     public void configure(Element metadata, Dictionary configuration) throws ConfigurationException
{
         Element[] controller = metadata.getElements("controller");
         String field = controller[0].getAttribute("field");   
-        getInstanceManager().register(this, new FieldMetadata[] {new FieldMetadata(field,
"boolean")}, null);
+        getInstanceManager().register(new FieldMetadata(field, "boolean"), this);
     }
 
     /**

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java
Mon Mar 17 11:40:47 2008
@@ -19,7 +19,6 @@
 package org.apache.felix.ipojo.handlers.providedservice;
 
 import java.lang.reflect.Field;
-import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -98,7 +97,6 @@
         m_providedServices = new ProvidedService[0];
         // Create the dependency according to the component metadata
         Element[] providedServices = componentMetadata.getElements("Provides");
-        List fields = new ArrayList();
         for (int i = 0; i < providedServices.length; i++) {
             String[] serviceSpecifications = ParseUtils.parseArrays(providedServices[i].getAttribute("interface"));
// Set by the initialize component factory.
             
@@ -135,7 +133,8 @@
                     }
                     
                     if (field != null) {
-                        fields.add(new FieldMetadata(field, type));
+                        getInstanceManager().register(new FieldMetadata(field, type), this);
+                        // Cannot register the property as the interception is necessary
to deal with registration update.
                     }
                 }
 
@@ -155,7 +154,6 @@
             }
 
         }
-        getInstanceManager().register(this, (FieldMetadata[]) fields.toArray(new FieldMetadata[fields.size()]),
null);
     }
     
     /**

Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Property.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Property.java?rev=638018&r1=638017&r2=638018&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Property.java
(original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Property.java
Mon Mar 17 11:40:47 2008
@@ -24,6 +24,7 @@
 
 import org.apache.felix.ipojo.ComponentInstance;
 import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.FieldInterceptor;
 import org.apache.felix.ipojo.Handler;
 import org.apache.felix.ipojo.InstanceManager;
 import org.apache.felix.ipojo.parser.ParseUtils;
@@ -34,7 +35,7 @@
  * This class allows storing property value and calling setter method too.
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
-public class Property {
+public class Property implements FieldInterceptor {
 
     /**
      * Name of the property (filed name if not set).
@@ -443,5 +444,29 @@
             m_handler.error("The method " + m_method + " in the class " + m_manager.getClassName()
+ "throws an exception : " + e.getTargetException().getMessage(), e.getTargetException());
             m_manager.setState(ComponentInstance.INVALID);
         }
+    }
+
+    /**
+     * A field value is required by the object 'pojo'.
+     * @param pojo : POJO object
+     * @param fieldName : field
+     * @param value : last value
+     * @return the value if the handler want to inject this value.
+     * @see org.apache.felix.ipojo.FieldInterceptor#onGet(java.lang.Object, java.lang.String,
java.lang.Object)
+     */
+    public Object onGet(Object pojo, String fieldName, Object value) {
+        return getValue();
+    }
+
+    /**
+     * The field 'field' receives a new value.
+     * @param pojo : pojo
+     * @param fieldName : field name
+     * @param value : new value
+     * @see org.apache.felix.ipojo.FieldInterceptor#onSet(java.lang.Object, java.lang.String,
java.lang.Object)
+     */
+    public void onSet(Object pojo, String fieldName, Object value) {
+        setValue(value);
+        
     }
 }



Mime
View raw message