Author: antelder
Date: Sat Jan 5 23:59:03 2008
New Revision: 609269
URL: http://svn.apache.org/viewvc?rev=609269&view=rev
Log:
Tidy up runtime launcher
Modified:
incubator/tuscany/java/sca/modules/runtime/src/main/java/org/apache/tuscany/sca/runtime/Launcher.java
Modified:
incubator/tuscany/java/sca/modules/runtime/src/main/java/org/apache/tuscany/sca/runtime/Launcher.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime/src/main/java/org/apache/tuscany/sca/runtime/Launcher.java?rev=609269&r1=609268&r2=609269&view=diff
==============================================================================
---
incubator/tuscany/java/sca/modules/runtime/src/main/java/org/apache/tuscany/sca/runtime/Launcher.java
(original)
+++
incubator/tuscany/java/sca/modules/runtime/src/main/java/org/apache/tuscany/sca/runtime/Launcher.java
Sat Jan 5 23:59:03 2008
@@ -21,17 +21,11 @@
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.net.MalformedURLException;
-import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
@@ -39,136 +33,154 @@
import javax.xml.namespace.QName;
-import org.apache.tuscany.sca.domain.DomainException;
import org.apache.tuscany.sca.node.NodeException;
import org.apache.tuscany.sca.node.SCANode;
import org.apache.tuscany.sca.node.SCANodeFactory;
/**
+ * A Launcher using a multiple nodes part as part of a domain
*/
public class Launcher {
private final static Logger logger =
Logger.getLogger(Launcher.class.getName());
- protected File repository;
-
- protected String nodeName;
+ protected SCANodeFactory scaNodeFactory;
+ protected List<SCANode> scaNodes;
protected String domainName;
- protected long hotDeployInterval; // miliseconds, 0 = no hot deploy
-
- protected SCANode node;
-
- protected Thread hotDeployThread;
- protected boolean stopHotDeployThread;
+ protected File repository;
- protected HashMap<URL, Long> existingContributions; // value is last
modified time
+ protected boolean started;
- public Launcher(File repository) {
- this(repository, null, null, false, 0);
- }
+ protected String cp;
- public Launcher(File repository, String nodeName, String domainName,
boolean startManager, long hotDeployInterval) {
+ public Launcher(File repository, String cp) {
this.repository = repository;
- this.nodeName = nodeName;
- this.domainName = domainName;
- this.hotDeployInterval = hotDeployInterval;
- }
+ this.cp = cp;
- public void start() throws NodeException, URISyntaxException,
InterruptedException, DomainException, MalformedURLException {
- logger.log(Level.INFO, "SCA runtime starting");
+ initFromPropertyFile();
+ logger.info("SCA runtime starting");
logger.info("repository: " + repository.getAbsolutePath());
-
- initFromPropertyFile();
- logger.info("nodeName: " + nodeName);
- logger.info("domainName: " + domainName);
- logger.info("hotDeployInterval: " + hotDeployInterval);
-
- node = startNode(nodeName, domainName);
-
- initHotDeploy(repository);
-
- logger.log(Level.INFO, "SCA runtime started");
+ logger.info("domain: " + ((domainName != null) ? domainName :
"STANDALONE"));
+
+ scaNodeFactory = SCANodeFactory.newInstance();
+ scaNodes = new ArrayList<SCANode>();
+
+ if (repository != null && repository.exists()) {
+ addTopLevelJARs(repository);
+ addSubFolders(repository);
+ }
+ }
+ public Launcher(File repository) {
+ this(repository, "http://localhost:8080/Tuscany");
}
- public void stop() {
- logger.log(Level.INFO, "SCA runtime stopping");
- stopHotDeployThread = true;
+ protected URL[] addTopLevelJARs(File repository) {
+ SCANode repoNode;
+ try {
+ repoNode = createNode(cp);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
- if (node != null) {
- stopNode(node);
+ URL[] jars = getJARsInFolder(repository);
+ for (URL jarURL : jars) {
+ try {
+ repoNode.addContribution(jarURL.toString(), jarURL);
+ logger.info("added contribution: " + jarURL);
+ } catch (NodeException e) {
+ e.printStackTrace();
+ logger.log(Level.WARNING, "exception adding contribution: " +
jarURL, e);
+ }
}
- logger.log(Level.INFO, "SCA runtime stopped");
+
+ return jars;
}
- protected SCANode startNode(String nodeName, String domainName) throws
NodeException, URISyntaxException, MalformedURLException {
- logger.log(Level.INFO, "starting node " + nodeName);
+ protected URL[] addSubFolders(File repository) {
+ URL[] folders = getSubFolders(repository);
+ for (URL folderURL : folders) {
+ try {
+ SCANode scaNode = createNode(cp + "/" + folderURL);
+ scaNode.addContribution(folderURL.toString(), folderURL);
+ logger.info("added contribution: " + folderURL);
+ } catch (Exception e) {
+ e.printStackTrace();
+ logger.log(Level.WARNING, "exception adding contribution: " +
folderURL, e);
+ }
+ }
+ return folders;
+ }
- SCANodeFactory nodeFactory = SCANodeFactory.newInstance();
- SCANode scaNode = nodeFactory.createSCANode(nodeName, domainName);
+ public void addContribution(URL contributionURL) throws NodeException {
+ SCANode scaNode = createNode(cp + "/" + contributionURL);
- initNode(scaNode);
- logger.log(Level.INFO, "started node " + nodeName);
+ if (started && domainName == null) {
+ scaNode.stop();
+ }
- return scaNode;
+ scaNode.addContribution(contributionURL.toString(), contributionURL);
+ logger.info("added contribution: " + contributionURL);
+
+ if (started) {
+ if (domainName == null) {
+ scaNode.addToDomainLevelComposite((QName)null);
+ scaNode.start();
+ } else {
+ scaNode.addToDomainLevelComposite((QName)null);
+ scaNode.start();
+ }
+ }
}
- protected void stopNode(SCANode node) {
- logger.log(Level.INFO, "stopping node " + node.getURI());
+ public void start() {
try {
- node.stop();
- logger.log(Level.INFO, "stopped node " + node.getURI());
+
+ for (SCANode scaNode : scaNodes) {
+ scaNode.addToDomainLevelComposite((QName)null);
+ scaNode.start();
+ }
+
} catch (NodeException e) {
- logger.log(Level.SEVERE, "exception stopping node " +
node.getURI(), e);
throw new RuntimeException(e);
}
+ started = true;
}
- protected void initNode(SCANode scaNode) throws NodeException,
URISyntaxException, MalformedURLException {
- existingContributions = new HashMap<URL, Long>();
-
- for (URL contribution : getContributionJarURLs(repository)) {
- scaNode.addContribution(contribution.toString(), contribution);
- existingContributions.put(contribution, new Long(new
File(contribution.toURI()).lastModified()));
- logger.log(Level.INFO, "Added contribution: " + contribution);
- }
-
- for (URL contribution : getContributionFolderURLs(repository)) {
- scaNode.addContribution(contribution.toString(), contribution);
- logger.log(Level.INFO, "Added contribution folder: " +
contribution);
- }
-
- scaNode.addToDomainLevelComposite((QName)null);
- scaNode.start();
- }
-
- protected void restartNode(SCANode scaNode) {
- stopNode(scaNode);
- for (URL contributionURL : existingContributions.keySet()) {
- try {
- scaNode.removeContribution(contributionURL.toString());
- } catch (NodeException e) {
- logger.log(Level.SEVERE, "exception removing contribution from
node: " + contributionURL, e);
+ public void stop() {
+ try {
+ for (SCANode scaNode : scaNodes) {
+ scaNode.stop();
}
+ } catch (NodeException e) {
+ throw new RuntimeException(e);
}
+ started = false;
+ }
+
+ public void destroy() {
try {
- initNode(scaNode);
- } catch (Throwable e) {
- logger.log(Level.SEVERE, "exception restarting node " +
scaNode.getURI(), e);
+ stop();
+ for (SCANode scaNode : scaNodes) {
+ scaNode.destroy();
+ }
+ } catch (NodeException e) {
+ throw new RuntimeException(e);
}
}
- protected URL[] getContributionJarURLs(File repositoryDir) {
+ protected URL[] getJARsInFolder(File repository) {
- String[] jarNames = repositoryDir.list(new FilenameFilter() {
+ String[] jarNames = repository.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
- }});
+ }
+ });
List<URL> contributionJars = new ArrayList<URL>();
if (jarNames != null) {
for (String jar : jarNames) {
try {
- contributionJars.add(new File(repositoryDir, jar).toURL());
+ contributionJars.add(new File(repository, jar).toURL());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
@@ -178,11 +190,12 @@
return contributionJars.toArray(new URL[contributionJars.size()]);
}
- protected URL[] getContributionFolderURLs(File repositoryDir) {
+ protected URL[] getSubFolders(File repositoryDir) {
String[] folderNames = repositoryDir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return new File(dir, name).isDirectory();
- }});
+ }
+ });
List<URL> contributionFolders = new ArrayList<URL>();
if (folderNames != null) {
@@ -194,102 +207,37 @@
}
}
}
-
+
return contributionFolders.toArray(new
URL[contributionFolders.size()]);
}
- protected void initHotDeploy(final File repository) {
-
- if (hotDeployInterval == 0) {
- return; // hotUpdateInterval of 0 disables hotupdate
- }
-
- Runnable runable = new Runnable() {
-
- public void run() {
- logger.info("Contribution hot deploy activated");
- while (!stopHotDeployThread) {
- try {
- Thread.sleep(hotDeployInterval);
- } catch (InterruptedException e) {
- }
- if (!stopHotDeployThread) {
- checkForUpdates(repository);
- }
- }
- logger.info("contribution hot deploy stopped");
+ /**
+ * Creates a new SCA Node unless running as a standalone node in
+ * which case just a single node is used to run for all contributions
+ */
+ protected SCANode createNode(String nodeName) throws NodeException {
+ SCANode scaNode;
+
+ if (domainName == null || domainName.length() < 1) {
+ if (scaNodes.size() < 1) {
+ scaNode = scaNodeFactory.createSCANode(nodeName, null);
+ scaNodes.add(scaNode);
+ } else {
+ scaNode = scaNodes.get(0);
}
- };
- hotDeployThread = new Thread(runable, "TuscanyHotDeploy");
- stopHotDeployThread = false;
- hotDeployThread.start();
- }
-
- protected void checkForUpdates(File repository) {
- URL[] currentContributions = getContributionJarURLs(repository);
- if (areContributionsAltered(currentContributions)) {
- restartNode(node);
+ } else {
+ scaNode = scaNodeFactory.createSCANode(nodeName, null);
+ scaNodes.add(scaNode);
}
- }
- protected boolean areContributionsAltered(URL[] currentContrabutions) {
- try {
-
- List addedContributions =
getAddedContributions(currentContrabutions);
- List removedContributions =
getRemovedContributions(currentContrabutions);
- List updatedContributions =
getUpdatedContributions(currentContrabutions);
-
- return (addedContributions.size() > 0 ||
removedContributions.size() > 0 || updatedContributions.size() > 0);
-
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- protected List<URL> getAddedContributions(URL[] currentContrabutions) {
- List<URL> urls = new ArrayList<URL>();
- for (URL url : currentContrabutions) {
- if (!existingContributions.containsKey(url)) {
- urls.add(url);
- }
- }
- return urls;
- }
-
- protected List<URL> getUpdatedContributions(URL[] currentContrabutions)
throws URISyntaxException {
- List<URL> urls = new ArrayList<URL>();
- for (URL url : currentContrabutions) {
- if (existingContributions.containsKey(url)) {
- File curentFile = new File(url.toURI());
- if (curentFile.lastModified() !=
existingContributions.get(url)) {
- urls.add(url);
- logger.info("updated contribution: " +
curentFile.getName());
- }
- }
- }
- return urls;
+ return scaNode;
}
- protected List getRemovedContributions(URL[] currentContrabutions) throws
URISyntaxException {
- List<URL> currentUrls = Arrays.asList(currentContrabutions);
- List<URL> urls = new ArrayList<URL>();
- for (URL url : existingContributions.keySet()) {
- if (!currentUrls.contains(url)) {
- urls.add(url);
- }
- }
- for (URL url : urls) {
- logger.info("removed contributions: " + new
File(url.toURI()).getName());
- }
- return urls;
- }
-
protected void initFromPropertyFile() {
File file = new File(repository, "tuscany.properties");
if (!file.exists()) {
return;
}
- logger.info("using config properties at: " + file);
Properties properties = new Properties();
try {
@@ -298,42 +246,124 @@
throw new RuntimeException(e);
}
- if (properties.getProperty("nodeName") != null) {
- this.nodeName = properties.getProperty("nodeName");
- }
- if (properties.getProperty("domainName") != null) {
+// if (properties.getProperty("nodeName") != null) {
+// this.nodeName = properties.getProperty("nodeName");
+// }
+ if (properties.getProperty("domainName") != null &&
properties.getProperty("domainName").length() > 0) {
this.domainName = properties.getProperty("domainName");
}
- if (properties.getProperty("hotDeployInterval") != null) {
- this.hotDeployInterval =
Long.parseLong(properties.getProperty("hotDeployInterval"));
- }
- }
-
- protected void copyFiles(File origin, File destination) throws IOException
{
- if (origin.isDirectory()) {
- if (!destination.exists()) {
- destination.mkdir();
- }
- for (String file : origin.list()) {
- copyFiles(new File(origin, file), new File(destination, file));
- }
- } else {
- InputStream in = new FileInputStream(origin);
- OutputStream out = new FileOutputStream(destination);
- try {
- byte[] buf = new byte[4096];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- } finally {
- in.close();
- out.close();
- }
- }
- }
-
- public SCANode getSCANode() {
- return node;
- }
+// if (properties.getProperty("hotDeployInterval") != null) {
+// this.hotDeployInterval =
Long.parseLong(properties.getProperty("hotDeployInterval"));
+// }
+ }
+
+// protected void initHotDeploy(final File repository) {
+//
+// if (hotDeployInterval == 0) {
+// return; // hotUpdateInterval of 0 disables hotupdate
+// }
+//
+// Runnable runable = new Runnable() {
+//
+// public void run() {
+// logger.info("Contribution hot deploy activated");
+// while (!stopHotDeployThread) {
+// try {
+// Thread.sleep(hotDeployInterval);
+// } catch (InterruptedException e) {
+// }
+// if (!stopHotDeployThread) {
+// checkForUpdates(repository);
+// }
+// }
+// logger.info("contribution hot deploy stopped");
+// }
+// };
+// hotDeployThread = new Thread(runable, "TuscanyHotDeploy");
+// stopHotDeployThread = false;
+// hotDeployThread.start();
+// }
+//
+// protected void checkForUpdates(File repository) {
+// URL[] currentContributions = getContributionJarURLs(repository);
+// if (areContributionsAltered(currentContributions)) {
+// restartNode(node);
+// }
+// }
+//
+// protected boolean areContributionsAltered(URL[] currentContrabutions) {
+// try {
+//
+// List addedContributions =
getAddedContributions(currentContrabutions);
+// List removedContributions =
getRemovedContributions(currentContrabutions);
+// List updatedContributions =
getUpdatedContributions(currentContrabutions);
+//
+// return (addedContributions.size() > 0 ||
removedContributions.size() > 0 || updatedContributions.size() > 0);
+//
+// } catch (URISyntaxException e) {
+// throw new RuntimeException(e);
+// }
+// }
+//
+// protected List<URL> getAddedContributions(URL[] currentContrabutions) {
+// List<URL> urls = new ArrayList<URL>();
+// for (URL url : currentContrabutions) {
+// if (!existingContributions.containsKey(url)) {
+// urls.add(url);
+// }
+// }
+// return urls;
+// }
+//
+// protected List<URL> getUpdatedContributions(URL[] currentContrabutions)
throws URISyntaxException {
+// List<URL> urls = new ArrayList<URL>();
+// for (URL url : currentContrabutions) {
+// if (existingContributions.containsKey(url)) {
+// File curentFile = new File(url.toURI());
+// if (curentFile.lastModified() !=
existingContributions.get(url)) {
+// urls.add(url);
+// logger.info("updated contribution: " +
curentFile.getName());
+// }
+// }
+// }
+// return urls;
+// }
+//
+// protected List getRemovedContributions(URL[] currentContrabutions)
throws URISyntaxException {
+// List<URL> currentUrls = Arrays.asList(currentContrabutions);
+// List<URL> urls = new ArrayList<URL>();
+// for (URL url : existingContributions.keySet()) {
+// if (!currentUrls.contains(url)) {
+// urls.add(url);
+// }
+// }
+// for (URL url : urls) {
+// logger.info("removed contributions: " + new
File(url.toURI()).getName());
+// }
+// return urls;
+// }
+//
+// protected void copyFiles(File origin, File destination) throws
IOException {
+// if (origin.isDirectory()) {
+// if (!destination.exists()) {
+// destination.mkdir();
+// }
+// for (String file : origin.list()) {
+// copyFiles(new File(origin, file), new File(destination,
file));
+// }
+// } else {
+// InputStream in = new FileInputStream(origin);
+// OutputStream out = new FileOutputStream(destination);
+// try {
+// byte[] buf = new byte[4096];
+// int len;
+// while ((len = in.read(buf)) > 0) {
+// out.write(buf, 0, len);
+// }
+// } finally {
+// in.close();
+// out.close();
+// }
+// }
+// }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]