Author: rfeng
Date: Fri May 15 18:36:42 2009
New Revision: 775283

URL: http://svn.apache.org/viewvc?rev=775283&view=rev
Log:
Push down some of the createNode() methods to NodeFactory and only require the 
createNode(NodeConfiguration) to be implemented by subclasses

Added:
    tuscany/java/sca/itest/contribution-folder/src/test/java/test/
    
tuscany/java/sca/itest/contribution-folder/src/test/java/test/ContributionFolderTestCase.java
      - copied, changed from r774852, 
tuscany/java/sca/itest/contribution-folder/src/test/java/ContributionFolderTestCase.java
Removed:
    
tuscany/java/sca/itest/contribution-folder/src/test/java/ContributionFolderTestCase.java
Modified:
    
tuscany/java/sca/itest/contribution-zip/src/test/java/org/apache/tuscany/sca/contribution/zip/ContributionZIPTestCase.java
    
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
    
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/ContributionLocationHelper.java
    
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java
    
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeMain2.java
    
tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java
    
tuscany/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java
    
tuscany/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java
    
tuscany/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java

Copied: 
tuscany/java/sca/itest/contribution-folder/src/test/java/test/ContributionFolderTestCase.java
 (from r774852, 
tuscany/java/sca/itest/contribution-folder/src/test/java/ContributionFolderTestCase.java)
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/itest/contribution-folder/src/test/java/test/ContributionFolderTestCase.java?p2=tuscany/java/sca/itest/contribution-folder/src/test/java/test/ContributionFolderTestCase.java&p1=tuscany/java/sca/itest/contribution-folder/src/test/java/ContributionFolderTestCase.java&r1=774852&r2=775283&rev=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/itest/contribution-folder/src/test/java/ContributionFolderTestCase.java
 (original)
+++ 
tuscany/java/sca/itest/contribution-folder/src/test/java/test/ContributionFolderTestCase.java
 Fri May 15 18:36:42 2009
@@ -1,4 +1,4 @@
-
+package test;
 
 import junit.framework.TestCase;
 
@@ -8,20 +8,18 @@
 
 import calculator.CalculatorService;
 
-
-
 /**
  * Test SCADomain.newInstance and invocation of a service.
- * 
+ *
  * @version $Rev: 608205 $ $Date: 2008-01-02 20:29:05 +0000 (Wed, 02 Jan 2008) 
$
  */
 public class ContributionFolderTestCase extends TestCase {
 
     private Node node;
-    
+
     @Override
     protected void setUp() throws Exception {
-               node = NodeFactory.newInstance().createNode(null, new 
Contribution("foo", "src/test/resources/repository"));
+        node = NodeFactory.newInstance().createNode(new Contribution("foo", 
"src/test/resources/repository"));
         node.start();
     }
 

Modified: 
tuscany/java/sca/itest/contribution-zip/src/test/java/org/apache/tuscany/sca/contribution/zip/ContributionZIPTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/itest/contribution-zip/src/test/java/org/apache/tuscany/sca/contribution/zip/ContributionZIPTestCase.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/itest/contribution-zip/src/test/java/org/apache/tuscany/sca/contribution/zip/ContributionZIPTestCase.java
 (original)
+++ 
tuscany/java/sca/itest/contribution-zip/src/test/java/org/apache/tuscany/sca/contribution/zip/ContributionZIPTestCase.java
 Fri May 15 18:36:42 2009
@@ -1,55 +1,56 @@
-/*
- * 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.tuscany.sca.contribution.zip;
-
-import junit.framework.TestCase;
-
-import org.apache.tuscany.sca.node.Contribution;
-import org.apache.tuscany.sca.node.Node;
-import org.apache.tuscany.sca.node.NodeFactory;
-
-import calculator.CalculatorService;
-
-
-/**
- * Test SCADomain.newInstance and invocation of a service.
- * 
- * @version $Rev: 608205 $ $Date: 2008-01-02 20:29:05 +0000 (Wed, 02 Jan 2008) 
$
- */
-public class ContributionZIPTestCase extends TestCase {
-
-    private Node node;
-    
-    @Override
-    protected void setUp() throws Exception {
-               node = NodeFactory.newInstance().createNode(null, new 
Contribution("foo", "src/test/resources/repository/contribution.zip"));
-        node.start();
-    }
-
-    public void testInvoke() throws Exception {
-        CalculatorService service = node.getService(CalculatorService.class, 
"CalculatorServiceComponent");
-        assertEquals(3.0, service.add(1, 2));
-    }
-
-    @Override
-    protected void tearDown() throws Exception {
-        node.stop();
-    }
-
-}
+/*
+ * 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.tuscany.sca.contribution.zip;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sca.node.Contribution;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+
+import calculator.CalculatorService;
+
+/**
+ * Test SCADomain.newInstance and invocation of a service.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ContributionZIPTestCase extends TestCase {
+
+    private Node node;
+
+    @Override
+    protected void setUp() throws Exception {
+        node =
+            NodeFactory.newInstance().createNode(new Contribution("foo",
+                                                                  
"src/test/resources/repository/contribution.zip"));
+        node.start();
+    }
+
+    public void testInvoke() throws Exception {
+        CalculatorService service = node.getService(CalculatorService.class, 
"CalculatorServiceComponent");
+        assertEquals(3.0, service.add(1, 2));
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        node.stop();
+    }
+
+}

Modified: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
 (original)
+++ 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
 Fri May 15 18:36:42 2009
@@ -6,24 +6,21 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.host.webapp;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -45,7 +42,6 @@
 import org.apache.tuscany.sca.node.Node;
 import org.apache.tuscany.sca.node.NodeFactory;
 import org.apache.tuscany.sca.node.impl.NodeImpl;
-import org.oasisopen.sca.ServiceRuntimeException;
 
 public class ServletHostHelper {
     private static final Logger logger = 
Logger.getLogger(ServletHostHelper.class.getName());
@@ -55,7 +51,7 @@
     public static void init(ServletConfig servletConfig) {
         init(servletConfig.getServletContext());
     }
-    
+
     public static ServletHost init(final ServletContext servletContext) {
         Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE);
         if (node == null) {
@@ -81,7 +77,7 @@
         }
         return getServletHost(node);
     }
-    
+
     private static WebAppServletHost getServletHost(Node node) {
         NodeImpl nodeImpl = (NodeImpl) node;
         ExtensionPointRegistry eps = nodeImpl.getExtensionPoints();
@@ -98,36 +94,17 @@
     }
 
     private static Node createNode(final ServletContext servletContext) throws 
ServletException {
-        String contextPath = initContextPath(servletContext);
+        // String contextPath = initContextPath(servletContext);
         String contributionRoot = getContributionRoot(servletContext);
         NodeFactory factory = NodeFactory.newInstance();
-        String webComposite = getWebComposite(servletContext);
-        Node node = factory.createNode(contextPath, webComposite, new 
Contribution(contributionRoot, contributionRoot));
+        InputStream webComposite = getWebComposite(servletContext);
+        Node node = factory.createNode(webComposite, new 
Contribution(contributionRoot, contributionRoot));
         node.start();
         return node;
     }
-    
-    private static String getWebComposite(ServletContext servletContext) {
-        InputStream stream = 
servletContext.getResourceAsStream("/WEB-INF/web.composite");
-        BufferedReader reader = new BufferedReader(new 
InputStreamReader(stream));
 
-        StringBuilder sb = new StringBuilder();
-        String s = null;
-        try {
-            while ((s = reader.readLine()) != null) {
-                sb.append(s + "\n");
-            }
-        } catch (IOException e) {
-            throw new ServiceRuntimeException(e);
-        } finally {
-            try {
-                stream.close();
-            } catch (IOException e) {
-                throw new ServiceRuntimeException(e);
-            }
-        }
- 
-        return sb.toString();
+    private static InputStream getWebComposite(ServletContext servletContext) {
+        return servletContext.getResourceAsStream("/WEB-INF/web.composite");
     }
 
     private static String getContributionRoot(ServletContext servletContext) {
@@ -144,7 +121,7 @@
             // ignore exception and use default location
 
             try {
-                
+
                 String root = 
servletContext.getInitParameter("contributionRoot");
                 if (root == null || root.length() < 1) {
                     root = "/";

Modified: 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/ContributionLocationHelper.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/ContributionLocationHelper.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/ContributionLocationHelper.java
 (original)
+++ 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/ContributionLocationHelper.java
 Fri May 15 18:36:42 2009
@@ -6,22 +6,28 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.node;
 
+import java.io.IOException;
 import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import org.oasisopen.sca.ServiceRuntimeException;
 
 /**
  * ContributionLocationHelper
@@ -32,7 +38,7 @@
 
     /**
      * Returns the location of the SCA contribution containing the given class.
-     * 
+     *
      * @param anchorClass
      * @return
      */
@@ -53,6 +59,15 @@
      */
     public static String getContributionLocation(String resourceName) {
         return getContributionLocation(null, resourceName);
+    }
+
+    /**
+     * Find the contribution locations by seraching a resource on the classpath
+     * @param resourceName
+     * @return
+     */
+    public static List<String> getContributionLocations(String resourceName) {
+        return getContributionLocations(null, resourceName);
 
     }
 
@@ -74,6 +89,32 @@
         if (resourceURL == null) {
             return null;
         }
+        return getRootLocation(resourceURL, resourceName);
+    }
+
+    /**
+     * Find the contribution locations by seraching a resource using the given 
classloader
+     * @param classLoader The classloader that is used to call getResources()
+     * @param resourceName The name of the resource
+     * @return A list of locations that contain the resource
+     */
+    public static List<String> getContributionLocations(ClassLoader 
classLoader, String resourceName) {
+        if (classLoader == null) {
+            classLoader = AccessController.doPrivileged(new 
PrivilegedAction<ClassLoader>() {
+                public ClassLoader run() {
+                    return Thread.currentThread().getContextClassLoader();
+                }
+            });
+        }
+        Enumeration<URL> resourceURLs = getResources(classLoader, 
resourceName);
+        List<String> locations = new ArrayList<String>();
+        while (resourceURLs != null && resourceURLs.hasMoreElements()) {
+            locations.add(getRootLocation(resourceURLs.nextElement(), 
resourceName));
+        }
+        return locations;
+    }
+
+    private static String getRootLocation(URL resourceURL, String 
resourceName) {
         String location = null;
         // "jar:file://....../something.jar!/a/b/c/app.composite"
         String url = resourceURL.toExternalForm();
@@ -98,4 +139,16 @@
         });
     }
 
+    private static Enumeration<URL> getResources(final ClassLoader 
classLoader, final String compositeURI) {
+        return AccessController.doPrivileged(new 
PrivilegedAction<Enumeration<URL>>() {
+            public Enumeration<URL> run() {
+                try {
+                    return classLoader.getResources(compositeURI);
+                } catch (IOException e) {
+                    throw new ServiceRuntimeException(e);
+                }
+            }
+        });
+    }
+
 }

Modified: 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java
 (original)
+++ 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java
 Fri May 15 18:36:42 2009
@@ -19,11 +19,22 @@
 
 package org.apache.tuscany.sca.node;
 
+import static 
org.apache.tuscany.sca.node.ContributionLocationHelper.getContributionLocations;
+
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.Reader;
 import java.lang.reflect.InvocationTargetException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.List;
 
 import 
org.apache.tuscany.sca.node.configuration.DefaultNodeConfigurationFactory;
 import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
+import org.apache.tuscany.sca.node.configuration.NodeConfigurationFactory;
 import org.oasisopen.sca.CallableReference;
 import org.oasisopen.sca.ServiceReference;
 import org.oasisopen.sca.ServiceRuntimeException;
@@ -35,6 +46,15 @@
  * @version $Rev$ $Date$
  */
 public abstract class NodeFactory extends DefaultNodeConfigurationFactory {
+    /**
+     * Default location of contribution metadata in an SCA contribution.
+     */
+    private static final String SCA_CONTRIBUTION_META = 
"META-INF/sca-contribution.xml";
+
+    /**
+     * Default location of a generated contribution metadata in an SCA 
contribution.
+     */
+    private static final String SCA_CONTRIBUTION_GENERATED_META = 
"META-INF/sca-contribution-generated.xml";
 
     protected static NodeFactory nodeFactory;
 
@@ -174,33 +194,109 @@
     }
 
     /**
-     * Creates a new SCA node using defaults for the contribution location and 
runnable composite
-     *
-     * @return a new SCA node.
+     * Open a URL connection without cache
+     * @param url
+     * @return
+     * @throws IOException
      */
-    public abstract Node createNode();
+    private static InputStream openStream(URL url) throws IOException {
+        InputStream is = null;
+        URLConnection connection = url.openConnection();
+        connection.setUseCaches(false);
+        is = connection.getInputStream();
+        return is;
+    }
+
+    /**
+     * Escape the space in URL string
+     * @param uri
+     * @return
+     */
+    private static URI createURI(String uri) {
+        int index = uri.indexOf(':');
+        String scheme = null;
+        String ssp = uri;
+        if (index != -1) {
+            scheme = uri.substring(0, index);
+            ssp = uri.substring(index + 1);
+        }
+        try {
+            return new URI(scheme, ssp, null);
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
 
     /**
      * Creates a new SCA node from the configuration URL
      *
-     * @param configurationURL the URL of the node configuration which is the 
ATOM feed
+     * @param configurationURL the URL of the node configuration which is the 
XML document
      * that contains the URI of the composite and a collection of URLs for the 
contributions
      *
      * @return a new SCA node.
      */
-    public abstract Node createNode(String configurationURL);
+    public Node createNode(URL configurationURL) {
+        try {
+            InputStream is = openStream(configurationURL);
+            return createNode(is);
+        } catch (IOException e) {
+            throw new ServiceRuntimeException(e);
+        }
+    }
+
+    /**
+     * Creates a new SCA node from the XML configuration of the node
+     * @param is The input stream that the XML configuration can be read. The 
stream will be closed
+     * after this call.
+     * @return a new SCA node
+     */
+    public Node createNode(InputStream is) {
+        try {
+            NodeConfiguration configuration = loadConfiguration(is);
+            is.close();
+            return createNode(configuration);
+        } catch (IOException e) {
+            throw new ServiceRuntimeException(e);
+        }
+    }
 
     /**
      * Creates a new SCA node.
      *
-     * @param compositeURI the URI of the composite to use
+     * @param deploymentCompositeURI the URI of the deployment composite. If 
the URI is relative, it should
+     * be resolved against the first contribution. Otherwise, the absolute URI 
is used to load the XML
+     * description of the composite. The deployment composite will be attached 
to the first contribution.
+     *
      * @param contributions the URI of the contributions that provides the 
composites and related
      * artifacts. If the list is empty, then we will use the thread context 
classloader to discover
      * the contribution on the classpath
      *
      * @return a new SCA node.
      */
-    public abstract Node createNode(String compositeURI, Contribution... 
contributions);
+    public Node createNode(String deploymentCompositeURI, Contribution... 
contributions) {
+        if (contributions == null || contributions.length == 0) {
+            if (deploymentCompositeURI == null || 
deploymentCompositeURI.indexOf(':') != -1) {
+                throw new ServiceRuntimeException("No SCA contribution is 
provided or discovered");
+            }
+            // Try to find contributions on the classpath by the composite URI
+            contributions = getContributions(getContributionLocations(null, 
deploymentCompositeURI));
+        }
+        NodeConfiguration configuration = createConfiguration(contributions);
+        if (deploymentCompositeURI != null && 
configuration.getContributions().size() > 0) {
+            
configuration.getContributions().get(0).addDeploymentComposite(createURI(deploymentCompositeURI));
+        }
+        return createNode(configuration);
+    }
+
+    /**
+     * Create a new SCA node using the list of SCA contributions
+     * @param contributions
+     * @return
+     */
+    public Node createNode(Contribution... contributions) {
+        NodeConfiguration configuration = createConfiguration(contributions);
+        return createNode(configuration);
+    }
 
     /**
      * Creates a new SCA node.
@@ -209,8 +305,87 @@
      * @param compositeContent the XML content of the composite to use
      * @param contributions the URI of the contributions that provides the 
composites and related artifacts
      * @return a new SCA node.
+     *
+     * @deprecated Please use createNode(InputStream compositeContent, 
Contribution... contributions) or
+     * createNode(Reader compositeContent, Contribution... contributions)
      */
-    public abstract Node createNode(String compositeURI, String 
compositeContent, Contribution... contributions);
+    @Deprecated
+    public Node createNode(String compositeURI, String compositeContent, 
Contribution... contributions) {
+        NodeConfiguration configuration = createConfiguration(contributions);
+        if (compositeContent != null && 
configuration.getContributions().size() > 0) {
+            
configuration.getContributions().get(0).addDeploymentComposite(compositeContent);
+        }
+        return createNode(configuration);
+    }
+
+    /**
+     * Creates a new SCA node.
+     *
+     * @param compositeContent the XML content of the deployment composite
+     * @param contributions the URI of the contributions that provides the 
composites and related artifacts
+     * @return a new SCA node.
+     */
+    public Node createNode(InputStream compositeContent, Contribution... 
contributions) {
+        NodeConfiguration configuration = createConfiguration(contributions);
+        if (compositeContent != null && 
configuration.getContributions().size() > 0) {
+            
configuration.getContributions().get(0).addDeploymentComposite(compositeContent);
+        }
+        return createNode(configuration);
+    }
+
+    /**
+     * Creates a new SCA node.
+     *
+     * @param compositeContent the XML content of the deployment composite
+     * @param contributions the URI of the contributions that provides the 
composites and related artifacts
+     * @return a new SCA node.
+     */
+    public Node createNode(Reader compositeContent, Contribution... 
contributions) {
+        NodeConfiguration configuration = createConfiguration(contributions);
+        if (compositeContent != null && 
configuration.getContributions().size() > 0) {
+            
configuration.getContributions().get(0).addDeploymentComposite(compositeContent);
+        }
+        return createNode(configuration);
+    }
+
+    private NodeConfiguration createConfiguration(Contribution... 
contributions) {
+        NodeConfigurationFactory factory = this;
+        NodeConfiguration configuration = factory.createNodeConfiguration();
+        if (contributions != null) {
+            for (Contribution c : contributions) {
+                configuration.addContribution(c.getURI(), c.getLocation());
+            }
+        }
+        return configuration;
+    }
+
+    /**
+     * Creates a new SCA node using defaults for the contribution location and 
deployable composites.
+     * By default, it uses the Thread context classloader to find 
META-INF/sca-contribution.xml or
+     * META-INF/sca-contribution-generated.xml on the classpath. The locations 
that contain such resources
+     * are taken as the SCA contributions.
+     *
+     * @return a new SCA node.
+     */
+    public Node createNode() {
+        List<String> locations = new ArrayList<String>();
+        locations.addAll(getContributionLocations(null, 
SCA_CONTRIBUTION_META));
+        locations.addAll(getContributionLocations(null, 
SCA_CONTRIBUTION_GENERATED_META));
+        Contribution[] contributions = getContributions(locations);
+        return createNode(contributions);
+    }
+
+    private Contribution[] getContributions(List<String> locations) {
+        if (locations.isEmpty()) {
+            throw new ServiceRuntimeException("No SCA contributions are found 
on the classpath");
+        }
+        Contribution[] contributions = new Contribution[locations.size()];
+        int index = 0;
+        for (String location : locations) {
+            contributions[index++] = new Contribution(location, location);
+        }
+        return contributions;
+    }
 
     /**
      * Create a new SCA node based on the configuration
@@ -225,4 +400,6 @@
      * @return The node configuration
      */
     public abstract NodeConfiguration loadConfiguration(InputStream xml);
+
+
 }

Modified: 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeMain2.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeMain2.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeMain2.java
 (original)
+++ 
tuscany/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeMain2.java
 Fri May 15 18:36:42 2009
@@ -6,15 +6,15 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.node;
@@ -24,13 +24,13 @@
 public class NodeMain2 {
 
     /**
-     * Start an SCA node 
+     * Start an SCA node
      * @param args a list of contribution jars for the node to run
      */
     public static void main(String[] args) throws Exception {
 
         Contribution[] contributions = new Contribution[args.length];
-        for (int i=0; i<args.length; i++) {
+        for (int i = 0; i < args.length; i++) {
             File f = new File(args[i]);
             if (!f.exists()) {
                 System.err.println("contribution not found: " + f);
@@ -39,18 +39,18 @@
             contributions[i] = new Contribution(f.toURI().toString(), 
f.toURI().toString());
         }
 
-        Node node = NodeFactory.newInstance().createNode(null, contributions);
-        node.start();
-        
+        Node node = 
NodeFactory.newInstance().createNode(contributions).start();
+
         System.out.println("Hit enter to stop node...");
         if (System.in.read() == -1) {
             // no sysin so wait for ever letting caller do the terminate
             Object lock = new Object();
-            synchronized(lock) {
+            synchronized (lock) {
                 lock.wait();
             }
         }
 
         node.stop();
+        node.destroy();
     }
 }

Modified: 
tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java
 (original)
+++ 
tuscany/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java
 Fri May 15 18:36:42 2009
@@ -6,21 +6,25 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.node.osgi.impl;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
-import java.util.logging.Logger;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
 
 import org.apache.tuscany.sca.contribution.Contribution;
 import org.apache.tuscany.sca.contribution.ContributionFactory;
@@ -31,7 +35,8 @@
  * @version $Rev: $ $Date: $
  */
 public class NodeUtil {
-    private static final Logger logger = 
Logger.getLogger(NodeFactoryImpl.class.getName());
+    private NodeUtil() {
+    }
 
     static Contribution contribution(ContributionFactory contributionFactory, 
org.apache.tuscany.sca.node.Contribution c) {
         Contribution contribution = contributionFactory.createContribution();
@@ -42,15 +47,37 @@
     }
 
     /**
+     * Open a URL connection without cache
+     * @param url
+     * @return
+     * @throws IOException
+     */
+    static InputStream openStream(URL url) throws IOException {
+        InputStream is = null;
+        URLConnection connection = url.openConnection();
+        connection.setUseCaches(false);
+        is = connection.getInputStream();
+        return is;
+    }
+
+    /**
      * Escape the space in URL string
      * @param uri
      * @return
      */
     static URI createURI(String uri) {
-        if (uri.indexOf(' ') != -1) {
-            uri = uri.replace(" ", "%20");
+        int index = uri.indexOf(':');
+        String scheme = null;
+        String ssp = uri;
+        if (index != -1) {
+            scheme = uri.substring(0, index);
+            ssp = uri.substring(index + 1);
+        }
+        try {
+            return new URI(scheme, ssp, null);
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException(e);
         }
-        return URI.create(uri);
     }
 
 }

Modified: 
tuscany/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java
 (original)
+++ 
tuscany/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java
 Fri May 15 18:36:42 2009
@@ -19,11 +19,7 @@
 
 package org.apache.tuscany.sca.node.impl;
 
-import static org.apache.tuscany.sca.node.impl.NodeUtil.openStream;
-
-import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
 
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLOutputFactory;
@@ -31,12 +27,9 @@
 
 import org.apache.tuscany.sca.extensibility.ServiceDeclaration;
 import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
-import org.apache.tuscany.sca.node.Contribution;
-import org.apache.tuscany.sca.node.ContributionLocationHelper;
 import org.apache.tuscany.sca.node.Node;
 import org.apache.tuscany.sca.node.NodeFactory;
 import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
-import org.apache.tuscany.sca.node.configuration.NodeConfigurationFactory;
 import 
org.apache.tuscany.sca.node.configuration.xml.NodeConfigurationProcessor;
 import org.oasisopen.sca.ServiceRuntimeException;
 
@@ -50,61 +43,6 @@
     }
 
     @Override
-    public Node createNode(String configurationURI) {
-        try {
-            URL url = new URL(configurationURI);
-            InputStream is = openStream(url);
-            NodeConfiguration configuration = loadConfiguration(is);
-            is.close();
-            return new NodeImpl(configuration);
-        } catch (IOException e) {
-            throw new ServiceRuntimeException(e);
-        }
-    }
-
-    @Override
-    public Node createNode(String compositeURI, Contribution... contributions) 
{
-        NodeConfigurationFactory factory = this;
-        NodeConfiguration configuration = factory.createNodeConfiguration();
-        for (Contribution c : contributions) {
-            configuration.addContribution(c.getURI(), c.getLocation());
-        }
-        if (compositeURI != null && configuration.getContributions().size() > 
0) {
-            
configuration.getContributions().get(0).addDeploymentComposite(NodeUtil.createURI(compositeURI));
-        }
-        return new NodeImpl(configuration);
-    }
-
-    @Override
-    public Node createNode(String compositeURI, String compositeContent, 
Contribution... contributions) {
-        NodeConfigurationFactory factory = this;
-        NodeConfiguration configuration = factory.createNodeConfiguration();
-        for (Contribution c : contributions) {
-            configuration.addContribution(c.getURI(), c.getLocation());
-        }
-        if (compositeContent != null && 
configuration.getContributions().size() > 0) {
-            
configuration.getContributions().get(0).addDeploymentComposite(compositeContent);
-        }
-        return new NodeImpl(configuration);
-    }
-
-    @Override
-    public Node createNode() {
-        String location =
-            ContributionLocationHelper
-                
.getContributionLocation(org.apache.tuscany.sca.contribution.Contribution.SCA_CONTRIBUTION_META);
-        if (location == null) {
-            location =
-                ContributionLocationHelper
-                    
.getContributionLocation(org.apache.tuscany.sca.contribution.Contribution.SCA_CONTRIBUTION_GENERATED_META);
-        }
-        if (location == null) {
-            throw new ServiceRuntimeException("No SCA contributions are found 
on the classpath");
-        }
-        return createNode(null, new 
Contribution("http://contributions/default";, location));
-    }
-
-    @Override
     public Node createNode(NodeConfiguration configuration) {
         return new NodeImpl(configuration);
     }

Modified: 
tuscany/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java
 (original)
+++ 
tuscany/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeImplTestCase.java
 Fri May 15 18:36:42 2009
@@ -22,6 +22,7 @@
 import hello.HelloWorld;
 
 import java.io.File;
+import java.io.StringReader;
 
 import org.apache.tuscany.sca.node.Contribution;
 import org.apache.tuscany.sca.node.Node;
@@ -47,8 +48,7 @@
     public void testNodeWithCompositeContent() {
         NodeFactory factory = new NodeFactoryImpl();
         Contribution contribution = new Contribution("c1", new 
File("target/test-classes").toURI().toString());
-        String compositeURI = "HelloWorld.composite";
-        Node node = factory.createNode(compositeURI, composite, contribution);
+        Node node = factory.createNode(new StringReader(composite), 
contribution);
         testNode2(node);
     }
 

Modified: 
tuscany/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java?rev=775283&r1=775282&r2=775283&view=diff
==============================================================================
--- 
tuscany/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java
 (original)
+++ 
tuscany/java/sca/samples/calculator/src/test/java/calculator/CalculatorTestCase.java
 Fri May 15 18:36:42 2009
@@ -6,15 +6,15 @@
  * 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.    
+ * under the License.
  */
 package calculator;
 
@@ -28,25 +28,22 @@
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.oasisopen.sca.annotation.EagerInit;
-import org.oasisopen.sca.annotation.Scope;
 
 /**
  * This shows how to test the Calculator service component.
  */
-...@scope("COMPOSITE") @EagerInit
 public class CalculatorTestCase{
-       private static Node node;
+    private static Node node;
     private static CalculatorService calculatorService;
-    
+
 
     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
        NodeFactory factory = NodeFactory.newInstance();
         String contribution = 
ContributionLocationHelper.getContributionLocation(CalculatorClient.class);
-        node = factory.createNode("Calculator.composite", new 
Contribution("calculator", contribution));
-        node.start();
-        
+        node = factory.createNode("Calculator.composite", new 
Contribution("calculator", contribution)).start();
+        // node = factory.createNode().start();
+
         calculatorService = ((Client)node).getService(CalculatorService.class, 
"CalculatorServiceComponent");
     }
 


Reply via email to