You are viewing a plain text version of this content. The canonical link for it is here.
Posted to s4-commits@incubator.apache.org by mm...@apache.org on 2012/01/03 14:03:28 UTC

[33/50] [abbrv] git commit: Moved server configuration to Module and moved sender/receiver outside the apps loop.

Moved server configuration to Module and moved sender/receiver outside the apps loop.


Project: http://git-wip-us.apache.org/repos/asf/incubator-s4/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-s4/commit/48f5c831
Tree: http://git-wip-us.apache.org/repos/asf/incubator-s4/tree/48f5c831
Diff: http://git-wip-us.apache.org/repos/asf/incubator-s4/diff/48f5c831

Branch: refs/heads/piper
Commit: 48f5c8317dcf1fcbc7ac846ad5f439269a083b53
Parents: 88c4a6d
Author: Leo Neumeyer <le...@s4.io>
Authored: Thu Oct 27 16:46:31 2011 -0700
Committer: Leo Neumeyer <le...@s4.io>
Committed: Thu Oct 27 16:46:31 2011 -0700

----------------------------------------------------------------------
 .../src/main/java/org/apache/s4/core/Module.java   |   19 ++++++---
 .../src/main/java/org/apache/s4/core/Server.java   |   30 +++++++-------
 .../s4-core/src/main/resources/s4-core.properties  |    1 +
 .../test/s4/core/apploading/AppLoadingTest.java    |   13 +++---
 4 files changed, 34 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/48f5c831/subprojects/s4-core/src/main/java/org/apache/s4/core/Module.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/main/java/org/apache/s4/core/Module.java b/subprojects/s4-core/src/main/java/org/apache/s4/core/Module.java
index 27fb386..799cff8 100644
--- a/subprojects/s4-core/src/main/java/org/apache/s4/core/Module.java
+++ b/subprojects/s4-core/src/main/java/org/apache/s4/core/Module.java
@@ -1,5 +1,6 @@
 package org.apache.s4.core;
 
+import java.io.File;
 import java.io.InputStream;
 
 import org.apache.commons.configuration.ConfigurationConverter;
@@ -23,8 +24,7 @@ public class Module extends AbstractModule {
     private void loadProperties(Binder binder) {
 
         try {
-            InputStream is = this.getClass().getResourceAsStream(
-                    "/s4-core.properties");
+            InputStream is = this.getClass().getResourceAsStream("/s4-core.properties");
             config = new PropertiesConfiguration();
             config.load(is);
 
@@ -32,8 +32,7 @@ public class Module extends AbstractModule {
             // TODO - validate properties.
 
             /* Make all properties injectable. Do we need this? */
-            Names.bindProperties(binder,
-                    ConfigurationConverter.getProperties(config));
+            Names.bindProperties(binder, ConfigurationConverter.getProperties(config));
         } catch (ConfigurationException e) {
             binder.addError(e);
             e.printStackTrace();
@@ -46,8 +45,14 @@ public class Module extends AbstractModule {
             loadProperties(binder());
 
         bind(Server.class).asEagerSingleton();
-//        bind(String.class).annotatedWith(Names.named("core.module"))
-//                .toInstance(config.getString("core.module"));
+
+        /*
+         * Apps dir is searched as follows: The s4.apps.path property in the properties file. The user's current working
+         * directory under the subdirectory /bin/apps.
+         */
+        String appsDir = config.getString("s4.apps.path", System.getProperty("user.dir") + File.separator + "bin"
+                + File.separator + "apps");
+        bind(String.class).annotatedWith(Names.named("appsDir")).toInstance(appsDir);
     }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/48f5c831/subprojects/s4-core/src/main/java/org/apache/s4/core/Server.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/main/java/org/apache/s4/core/Server.java b/subprojects/s4-core/src/main/java/org/apache/s4/core/Server.java
index 20a2e52..f736d88 100644
--- a/subprojects/s4-core/src/main/java/org/apache/s4/core/Server.java
+++ b/subprojects/s4-core/src/main/java/org/apache/s4/core/Server.java
@@ -21,9 +21,8 @@ import com.google.inject.Injector;
 import com.google.inject.name.Named;
 
 /**
- * The Server instance coordinates activities in a cluster node including
- * loading and unloading of applications and instantiating the communication
- * layer.
+ * The Server instance coordinates activities in a cluster node including loading and unloading of applications and
+ * instantiating the communication layer.
  */
 public class Server {
 
@@ -32,19 +31,20 @@ public class Server {
     final private String commModuleName;
     final private String logLevel;
     public static final String MANIFEST_S4_APP_CLASS = "S4-App-Class";
-    // NOTE: currently we use a directory, but this will be changed by a URL (ref to zookeeper?), 
+    // NOTE: currently we use a directory, but this will be changed by a URL (ref to zookeeper?),
     // so that applications can be downloaded from a remote repository
-    final private static String S4_APPS_PATH = System.getProperty("s4.apps.path", System.getProperty("user.dir")
-            + "/bin/apps");
+    final private String appsDir;
     List<App> apps = new ArrayList<App>();
 
     /**
      * 
      */
     @Inject
-    public Server(@Named("comm.module") String commModuleName, @Named("s4.logger_level") String logLevel) {
+    public Server(@Named("comm.module") String commModuleName, @Named("s4.logger_level") String logLevel,
+            @Named("appsDir") String appsDir) {
         this.commModuleName = commModuleName;
         this.logLevel = logLevel;
+        this.appsDir = appsDir;
     }
 
     public void start() throws Exception {
@@ -67,23 +67,25 @@ public class Server {
         /* After some indirection we get the injector. */
         injector = Guice.createInjector(module);
 
-        File[] s4rFiles = new File(S4_APPS_PATH).listFiles(new PatternFilenameFilter("\\w+\\.s4r"));
+        Sender sender = injector.getInstance(Sender.class);
+        Receiver receiver = injector.getInstance(Receiver.class);
+
+        File[] s4rFiles = new File(appsDir).listFiles(new PatternFilenameFilter("\\w+\\.s4r"));
         for (File s4rFile : s4rFiles) {
-            loadApp(injector, s4rFile);
+            loadApp(sender, receiver, s4rFile);
         }
 
-        // now init + start apps
+        /* Now init + start apps. TODO: implement dynamic loading/unloading using ZK. */
         for (App app : apps) {
             logger.info("Starting app " + app.getClass().getName());
             app.init();
             app.start();
         }
 
-        logger.info("Completed applications startup");
-
+        logger.info("Completed applications startup.");
     }
 
-    private void loadApp(Injector injector, File s4r) {
+    private void loadApp(Sender sender, Receiver receiver, File s4r) {
 
         S4RLoader cl = new S4RLoader(s4r.getAbsolutePath());
         try {
@@ -108,8 +110,6 @@ public class Server {
                 return;
             }
 
-            Sender sender = injector.getInstance(Sender.class);
-            Receiver receiver = injector.getInstance(Receiver.class);
             app.setCommLayer(sender, receiver);
             apps.add(app);
         } catch (IOException e) {

http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/48f5c831/subprojects/s4-core/src/main/resources/s4-core.properties
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/main/resources/s4-core.properties b/subprojects/s4-core/src/main/resources/s4-core.properties
index 1a075f4..7db6157 100644
--- a/subprojects/s4-core/src/main/resources/s4-core.properties
+++ b/subprojects/s4-core/src/main/resources/s4-core.properties
@@ -1,4 +1,5 @@
 comm.module = org.apache.s4.comm.Module
 s4.logger_level = TRACE
+#s4.apps.path = /My/Apps/Dir
 
 

http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/48f5c831/subprojects/s4-core/src/test/java/test/s4/core/apploading/AppLoadingTest.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/test/java/test/s4/core/apploading/AppLoadingTest.java b/subprojects/s4-core/src/test/java/test/s4/core/apploading/AppLoadingTest.java
index d0cf3a8..e88aedf 100644
--- a/subprojects/s4-core/src/test/java/test/s4/core/apploading/AppLoadingTest.java
+++ b/subprojects/s4-core/src/test/java/test/s4/core/apploading/AppLoadingTest.java
@@ -12,7 +12,6 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
-import java.util.jar.Manifest;
 
 import org.apache.s4.core.Server;
 import org.apache.zookeeper.ZooKeeper;
@@ -63,7 +62,8 @@ public class AppLoadingTest {
     public void testA() throws Exception, InterruptedException {
 
         // add all classes from counter app
-        File rootAppDir = new File ( new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath()+"/s4-example/bin");
+        File rootAppDir = new File(new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath()
+                + "/s4-example/bin");
         File appFilesDir = new File(rootAppDir, "org/apache/s4/example/counter");
         generateS4RFromDirectoryContents(rootAppDir, appFilesDir, "counterExample",
                 "org.apache.s4.example.counter.MyApp");
@@ -118,16 +118,15 @@ public class AppLoadingTest {
 
     /**
      * 
-     * 1. generates an s4r package from classes in the apploading package (TODO process still to be improved), 
-     * 2. deploys it to bin/apps 
-     * 3. starts a forked S4 node, which loads apps from bin/apps
-     * 4. verifies app is working (s4 app started, event correctly processed)
+     * 1. generates an s4r package from classes in the apploading package (TODO process still to be improved), 2.
+     * deploys it to bin/apps 3. starts a forked S4 node, which loads apps from bin/apps 4. verifies app is working (s4
+     * app started, event correctly processed)
      * 
      * NOTE: we'll need to add an automatic test for which we make sure code cannot be in the classpath
      */
     @Test
     public void testAppLoading() throws Exception {
-        
+
         // TODO fix paths
 
         final ZooKeeper zk = TestUtils.createZkClient();