Author: jboynes
Date: Sat Apr  8 14:14:09 2006
New Revision: 392608

URL: http://svn.apache.org/viewcvs?rev=392608&view=rev
Log:
enable monitoring in StAXLoaderRegistry
tell HelloWorld sample to use JSR47 logging

Modified:
    
incubator/tuscany/java/samples/helloworld/helloworld/src/main/java/org/apache/tuscany/samples/helloworld/HelloWorldClient.java
    
incubator/tuscany/java/samples/helloworld/helloworld/src/test/java/org/apache/tuscany/samples/helloworld/HelloWorldServiceComponentTestCase.java
    
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
    
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/ComponentTypeLoaderTestCase.java

Modified: 
incubator/tuscany/java/samples/helloworld/helloworld/src/main/java/org/apache/tuscany/samples/helloworld/HelloWorldClient.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/samples/helloworld/helloworld/src/main/java/org/apache/tuscany/samples/helloworld/HelloWorldClient.java?rev=392608&r1=392607&r2=392608&view=diff
==============================================================================
--- 
incubator/tuscany/java/samples/helloworld/helloworld/src/main/java/org/apache/tuscany/samples/helloworld/HelloWorldClient.java
 (original)
+++ 
incubator/tuscany/java/samples/helloworld/helloworld/src/main/java/org/apache/tuscany/samples/helloworld/HelloWorldClient.java
 Sat Apr  8 14:14:09 2006
@@ -16,21 +16,29 @@
  */
 package org.apache.tuscany.samples.helloworld;
 
+import java.util.logging.Level;
+import java.util.Properties;
+
 import org.osoa.sca.CurrentModuleContext;
 import org.osoa.sca.ModuleContext;
 
 import org.apache.tuscany.core.client.TuscanyRuntime;
+import org.apache.tuscany.common.monitor.MonitorFactory;
+import org.apache.tuscany.common.monitor.impl.JavaLoggingMonitorFactory;
 
 /**
- * This client program shows how to create an SCA runtime, start it, locate a 
simple HelloWorld service component
- * and invoke it.
+ * This client program shows how to create an SCA runtime, start it,
+ * locate a simple HelloWorld service component and invoke it.
  */
 public class HelloWorldClient {
 
     public static final void main(String[] args) throws Exception {
+        // Setup Tuscany monitoring to use java.util.logging 
+        Properties levels = new Properties();
+        MonitorFactory monitorFactory = new JavaLoggingMonitorFactory(levels, 
Level.INFO, null);
 
         // Obtain Tuscany runtime
-        TuscanyRuntime tuscany = new TuscanyRuntime("hello", null);
+        TuscanyRuntime tuscany = new TuscanyRuntime("hello", null, 
monitorFactory);
 
         // Start the runtime
         tuscany.start();

Modified: 
incubator/tuscany/java/samples/helloworld/helloworld/src/test/java/org/apache/tuscany/samples/helloworld/HelloWorldServiceComponentTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/samples/helloworld/helloworld/src/test/java/org/apache/tuscany/samples/helloworld/HelloWorldServiceComponentTestCase.java?rev=392608&r1=392607&r2=392608&view=diff
==============================================================================
--- 
incubator/tuscany/java/samples/helloworld/helloworld/src/test/java/org/apache/tuscany/samples/helloworld/HelloWorldServiceComponentTestCase.java
 (original)
+++ 
incubator/tuscany/java/samples/helloworld/helloworld/src/test/java/org/apache/tuscany/samples/helloworld/HelloWorldServiceComponentTestCase.java
 Sat Apr  8 14:14:09 2006
@@ -26,54 +26,7 @@
  * 
  */
 public class HelloWorldServiceComponentTestCase extends TestCase {
-    public boolean verbose = true;
-
     public void testGeetings() throws Exception {
-
-        if (verbose)
-            System.out.println("starting test..");
-        System.out.flush();
-
-
-        TuscanyRuntime tuscany = new TuscanyRuntime("test", null);
-        tuscany.start();
-        ModuleContext moduleContext = CurrentModuleContext.getContext();
-
-        assertNotNull(moduleContext);
-        System.out.println("module context name '"
-                + moduleContext.getName() + "'");
-        System.out.println("module context uri '" + moduleContext.getURI()
-                + "'");
-        HelloWorldService helloworldService = (HelloWorldService) 
moduleContext.locateService("HelloWorldServiceComponent");
-
-        assertNotNull(helloworldService);
-
-
-        String value = helloworldService
-                .getGreetings("World");
-
-        if (verbose)
-            System.out.println("Value = '" + value + "'");
-
-        assertEquals("Hello World", value);
-
-        tuscany.stop();
-
+        HelloWorldClient.main(null);
     }
-
-    public final static void main(String[] args) throws Exception {
-        HelloWorldServiceComponentTestCase hwc = new 
HelloWorldServiceComponentTestCase();
-        hwc.setVerbose(true);
-        hwc.testGeetings();
-
-    }
-
-    public boolean isVerbose() {
-        return verbose;
-    }
-
-    public void setVerbose(boolean verbose) {
-        this.verbose = verbose;
-    }
-
 }

Modified: 
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java?rev=392608&r1=392607&r2=392608&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
 (original)
+++ 
incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/monitor/impl/JavaLoggingMonitorFactory.java
 Sat Apr  8 14:14:09 2006
@@ -20,21 +20,21 @@
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
-import java.util.WeakHashMap;
-import java.util.Map;
 import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
-import java.util.logging.Logger;
+import java.util.WeakHashMap;
 import java.util.logging.Level;
+import java.util.logging.Logger;
 
-import org.apache.tuscany.common.monitor.MonitorFactory;
 import org.apache.tuscany.common.monitor.LogLevel;
+import org.apache.tuscany.common.monitor.MonitorFactory;
 
 /**
  * A factory for monitors that forwards events to a [EMAIL PROTECTED] 
java.util.logging.Logger Java Logging (JSR47) Logger}.
  *
- * @see java.util.logging
  * @version $Rev$ $Date$
+ * @see java.util.logging
  */
 public class JavaLoggingMonitorFactory implements MonitorFactory {
     private final String bundleName;
@@ -52,9 +52,9 @@
      * <code>Class.getName() + '#' + Method.getName()</code> and the value the 
log level to use
      * as defined by [EMAIL PROTECTED] java.util.logging.Level}.
      *
-     * @param levels definition of custom levels for specific monitored methods
+     * @param levels       definition of custom levels for specific monitored 
methods
      * @param defaultLevel the default log level to use
-     * @param bundleName the name of a resource bundle that will be passed to 
the logger
+     * @param bundleName   the name of a resource bundle that will be passed 
to the logger
      * @see java.util.logging.Logger
      */
     public JavaLoggingMonitorFactory(Properties levels, Level defaultLevel, 
String bundleName) {
@@ -75,7 +75,7 @@
     public synchronized <T> T getMonitor(Class<T> monitorInterface) {
         T proxy = getCachedMonitor(monitorInterface);
         if (proxy == null) {
-            proxy = createMonitor(monitorInterface);
+            proxy = createMonitor(monitorInterface, bundleName);
             proxies.put(monitorInterface, new WeakReference<T>(proxy));
         }
         return proxy;
@@ -86,7 +86,7 @@
         return (ref != null) ? monitorInterface.cast(ref.get()) : null;
     }
 
-    private <T>T createMonitor(Class<T> monitorInterface) {
+    private <T>T createMonitor(Class<T> monitorInterface, String bundleName) {
         String className = monitorInterface.getName();
         Logger logger = Logger.getLogger(className, bundleName);
         Method[] methods = monitorInterface.getMethods();
@@ -107,9 +107,10 @@
                     }
                 }
             }
-            if (level != null) {
-                levels.put(method.getName(), level);
+            if (level == null) {
+                level = defaultLevel;
             }
+            levels.put(method.getName(), level);
         }
         InvocationHandler handler = new LoggingHandler(logger, levels);
         return 
monitorInterface.cast(Proxy.newProxyInstance(monitorInterface.getClassLoader(), 
new Class<?>[]{monitorInterface}, handler));

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java?rev=392608&r1=392607&r2=392608&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
 Sat Apr  8 14:14:09 2006
@@ -35,16 +35,28 @@
 public class StAXLoaderRegistryImpl implements StAXLoaderRegistry {
     private final Map<QName, StAXElementLoader<? extends AssemblyModelObject>> 
loaders = new HashMap<QName, StAXElementLoader<? extends 
AssemblyModelObject>>();
 
+    private Monitor monitor;
+
+    @org.apache.tuscany.core.system.annotation.Monitor
+    public void setMonitor(Monitor monitor) {
+        this.monitor = monitor;
+    }
+
     public <T extends AssemblyModelObject> void 
registerLoader(StAXElementLoader<T> loader) {
-        loaders.put(loader.getXMLType(), loader);
+        QName xmlType = loader.getXMLType();
+        monitor.registeringLoader(xmlType);
+        loaders.put(xmlType, loader);
     }
 
     public <T extends AssemblyModelObject> void 
unregisterLoader(StAXElementLoader<T> loader) {
-        loaders.remove(loader.getXMLType());
+        QName xmlType = loader.getXMLType();
+        monitor.unregisteringLoader(xmlType);
+        loaders.remove(xmlType);
     }
 
     public AssemblyModelObject load(XMLStreamReader reader, ResourceLoader 
resourceLoader) throws XMLStreamException, ConfigurationLoadException {
         QName name = reader.getName();
+        monitor.elementLoad(name);
         StAXElementLoader<? extends AssemblyModelObject> loader = 
loaders.get(name);
         if (loader == null) {
             throw new ConfigurationLoadException("Unrecognized element: " + 
name);
@@ -64,5 +76,28 @@
     @Deprecated
     public void setContext(AssemblyModelContext context) {
         modelContext.set(context);
+    }
+
+    public static interface Monitor {
+        /**
+         * Event emitted when a StAX element loader is registered.
+         *
+         * @param xmlType the QName of the element the loader will handle
+         */
+        void registeringLoader(QName xmlType);
+
+        /**
+         * Event emitted when a StAX element loader is unregistered.
+         *
+         * @param xmlType the QName of the element the loader will handle
+         */
+        void unregisteringLoader(QName xmlType);
+
+        /**
+         * Event emitted when a request is made to load an element.
+         *
+         * @param xmlType the QName of the element that should be loaded
+         */
+        void elementLoad(QName xmlType);
     }
 }

Modified: 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/ComponentTypeLoaderTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/ComponentTypeLoaderTestCase.java?rev=392608&r1=392607&r2=392608&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/ComponentTypeLoaderTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/ComponentTypeLoaderTestCase.java
 Sat Apr  8 14:14:09 2006
@@ -31,13 +31,14 @@
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
+import javax.xml.namespace.QName;
 
 /**
  * @version $Rev$ $Date$
  */
 public class ComponentTypeLoaderTestCase extends TestCase {
     private XMLInputFactory xmlFactory;
-    private StAXLoaderRegistry registry;
+    private StAXLoaderRegistryImpl registry;
     private SystemAssemblyFactoryImpl assemblyFactory;
     private ResourceLoaderImpl resourceLoader;
 
@@ -58,6 +59,7 @@
         xmlFactory = XMLInputFactory.newInstance();
         assemblyFactory = new SystemAssemblyFactoryImpl();
         registry = new StAXLoaderRegistryImpl();
+        registry.setMonitor(NULL_MONITOR);
         register(new ComponentTypeLoader());
         register(new ServiceLoader());
         resourceLoader = new ResourceLoaderImpl(getClass().getClassLoader());
@@ -68,4 +70,15 @@
         loader.setRegistry(registry);
         loader.start();
     }
+
+    private static final StAXLoaderRegistryImpl.Monitor NULL_MONITOR = new 
StAXLoaderRegistryImpl.Monitor() {
+        public void registeringLoader(QName xmlType) {
+        }
+
+        public void unregisteringLoader(QName xmlType) {
+        }
+
+        public void elementLoad(QName xmlType) {
+        }
+    };
 }


Reply via email to