You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@river.apache.org by gt...@apache.org on 2012/05/03 08:21:20 UTC

svn commit: r1333320 - in /river/jtsk/skunk/surrogate: src/org/apache/river/container/ src/org/apache/river/container/deployer/ test/org/apache/river/container/deployer/ testfiles/testroot/profile/default/

Author: gtrasuk
Date: Thu May  3 06:21:20 2012
New Revision: 1333320

URL: http://svn.apache.org/viewvc?rev=1333320&view=rev
Log:
Ongoing work on starter service deployer.

Added:
    river/jtsk/skunk/surrogate/testfiles/testroot/profile/default/service-starter.cfg
      - copied unchanged from r1327138, river/jtsk/skunk/surrogate/test/org/apache/river/container/deployer/sample.config
Removed:
    river/jtsk/skunk/surrogate/testfiles/testroot/profile/default/service-starter.properties
Modified:
    river/jtsk/skunk/surrogate/src/org/apache/river/container/SurrogateInstaller.java
    river/jtsk/skunk/surrogate/src/org/apache/river/container/deployer/StarterServiceDeployer.java
    river/jtsk/skunk/surrogate/test/org/apache/river/container/deployer/DeployerConfigParserTest.java
    river/jtsk/skunk/surrogate/testfiles/testroot/profile/default/config.xml

Modified: river/jtsk/skunk/surrogate/src/org/apache/river/container/SurrogateInstaller.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/surrogate/src/org/apache/river/container/SurrogateInstaller.java?rev=1333320&r1=1333319&r2=1333320&view=diff
==============================================================================
--- river/jtsk/skunk/surrogate/src/org/apache/river/container/SurrogateInstaller.java (original)
+++ river/jtsk/skunk/surrogate/src/org/apache/river/container/SurrogateInstaller.java Thu May  3 06:21:20 2012
@@ -15,7 +15,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.river.container;
 
 import org.apache.river.container.deployer.ApplicationEnvironment;
@@ -26,25 +25,26 @@ import java.io.File;
  * @author trasukg
  */
 public class SurrogateInstaller {
-    /**
-     Create and install a surrogate based on a surrogate file which has
-     been unpacked into a working directory.
 
-     <p>This seems to be a lot like installing a generic application, apart
-     from the specific ways of determining the surrogate's class, so
-     one wonders whether we might eventually make this a plain application
-     loader, and separate out the surrogate-specific items into the
-     connector.
-     </p>
-     <p>More formally then, the surrogate connector could read a surrogate
-     file, then create a plain application that represents the surrogate.
-     </p>
-     <p>In that case, the surrogate application is simply a plain application
-     that has some extra limitations placed on it (e.g. no access to
-     local resources, a more restrictive security policy, etc).
-     </p>
-
-     @param workingDir
+    /**
+    Create and install a surrogate based on a surrogate file which has
+    been unpacked into a working directory.
+    
+    <p>This seems to be a lot like installing a generic application, apart
+    from the specific ways of determining the surrogate's class, so
+    one wonders whether we might eventually make this a plain application
+    loader, and separate out the surrogate-specific items into the
+    connector.
+    </p>
+    <p>More formally then, the surrogate connector could read a surrogate
+    file, then create a plain application that represents the surrogate.
+    </p>
+    <p>In that case, the surrogate application is simply a plain application
+    that has some extra limitations placed on it (e.g. no access to
+    local resources, a more restrictive security policy, etc).
+    </p>
+    
+    @param workingDir
      */
     void installSurrogate(Host host, File workingDir) {
         // Create a context for the surrogate.
@@ -56,17 +56,17 @@ public class SurrogateInstaller {
 
             /* Startup the application environment. */
             // Initialize the class loader with the surrogate's classes
-            /* TODO: Set the parent classloader to what? */
+            /* TODO: Set the parent classloader to what? 
             SettableCodebaseClassLoader classLoader =
-                    SettableCodebaseClassLoader.createLoader(null, workingDir);
+            SettableCodebaseClassLoader.createLoader(null, workingDir);
             appEnv.setClassLoader(classLoader);
-
+             */
             // Instantiate the surrogate.
             // Try the surrogate's getCodebase method to find the codebase
             /*
-             If codebase method returns nothing,
-             read the manifest to get the
-             codebase entries.
+            If codebase method returns nothing,
+            read the manifest to get the
+            codebase entries.
              */
             /*
              * Setup the discovery manager for the surrogate.
@@ -90,7 +90,6 @@ public class SurrogateInstaller {
     }
 
     private void configureApplicationEnvironment(ApplicationEnvironment appEnv, File workingDir) {
-            /* Read the manifest to get the surrogate's class. */
+        /* Read the manifest to get the surrogate's class. */
     }
-
 }

Modified: river/jtsk/skunk/surrogate/src/org/apache/river/container/deployer/StarterServiceDeployer.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/surrogate/src/org/apache/river/container/deployer/StarterServiceDeployer.java?rev=1333320&r1=1333319&r2=1333320&view=diff
==============================================================================
--- river/jtsk/skunk/surrogate/src/org/apache/river/container/deployer/StarterServiceDeployer.java (original)
+++ river/jtsk/skunk/surrogate/src/org/apache/river/container/deployer/StarterServiceDeployer.java Thu May  3 06:21:20 2012
@@ -39,6 +39,7 @@ import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import net.jini.security.policy.DynamicPolicyProvider;
+import net.jini.security.Security;
 import org.apache.commons.vfs.FileObject;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileType;
@@ -141,8 +142,8 @@ public class StarterServiceDeployer {
 
     public VirtualFileSystemClassLoader createServiceClassloader(FileObject serviceRoot, CodeSource codeSource) throws IOException, FileSystemException {
 
-        String parentLoaderName=((ASTparent) configNode.search(
-                new Class[]{ ASTconfig.class, ASTclassloader.class, ASTparent.class})).getValue().toString();
+        String parentLoaderName = configNode.search(
+                new Class[]{ASTconfig.class, ASTclassloader.class, ASTparent.class}).get(0).jjtGetChild(0).toString();
         log.log(Level.FINE, MessageNames.SERVICE_PARENT_CLASSLOADER_IS, parentLoaderName);
         ClassLoader parentLoader= (ClassLoader) context.get(parentLoaderName);
         VirtualFileSystemClassLoader cl =
@@ -152,7 +153,7 @@ public class StarterServiceDeployer {
         */
         ASTclasspath platformJarSpec = (ASTclasspath)
                 configNode.search(new Class[]{ASTconfig.class, 
-                    ASTclassloader.class, ASTjars.class, ASTclasspath.class});
+                    ASTclassloader.class, ASTjars.class, ASTclasspath.class}).get(0);
         addPlatformJarsToClassloader(platformJarSpec, cl);
         addLibDirectoryJarsToClasspath(serviceRoot, cl);
         
@@ -330,7 +331,7 @@ public class StarterServiceDeployer {
         }
     }
 
-    private void deployServiceArchive(FileObject serviceArchive) throws IOException {
+    private void deployServiceArchive(FileObject serviceArchive) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
         // Create an application environment
         ApplicationEnvironment env=new ApplicationEnvironment();
         env.setServiceArchive(serviceArchive);
@@ -356,7 +357,7 @@ public class StarterServiceDeployer {
         return serviceRoot.getURL();
     }
 
-    private void deployService(ApplicationEnvironment env) throws IOException {
+    private void deployService(ApplicationEnvironment env) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
         String serviceName = findServiceName(env.getServiceArchive(), env.getServiceRoot());
         CodeSource serviceCodeSource =
                 new CodeSource(findServiceURL(env.getServiceArchive(), env.getServiceRoot()),
@@ -381,8 +382,8 @@ public class StarterServiceDeployer {
          Grant the appropriate permissions to the service's classloader and
          protection domain.
          */
-
-        // TODO: Figure out how the protection domains, codesources, etc need to work.
+        Permission[] perms=createPermissionsInClassloader(cl);
+        grantPermissions(cl, perms);
         setupLiaisonConfiguration(env.getServiceArchive(), env.getServiceRoot(), cl);
         Properties startProps = readStartProperties(env.getServiceRoot());
         String argLine = startProps.getProperty(Strings.START_PARAMETERS);
@@ -392,6 +393,24 @@ public class StarterServiceDeployer {
         log.log(Level.INFO, MessageNames.COMPLETED_SERVICE_DEPLOYMENT, serviceName);
     }
 
+    Permission[] createPermissionsInClassloader(ClassLoader cl) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
+        List<Permission> perms=new ArrayList<Permission>();
+        // Get all the permission nodes from the config.
+        Class[] path=new Class[]{ ASTconfig.class, ASTgrant.class, ASTpermission.class};
+        List<ASTNode> permNodes=configNode.search(path);
+        // Create a permission for each
+        for(ASTNode node: permNodes) {
+            String className=(String)((ASTsymbol) node.jjtGetChild(0)).getValue();
+            Object permissionConstructorArgs[]=new String[node.jjtGetNumChildren()-1];
+            for (int i = 0; i < permissionConstructorArgs.length; i++) {
+                permissionConstructorArgs[i]=(String)((ASTsymbol) node.jjtGetChild(i+1)).getValue();     
+            }
+            Permission perm=(Permission) invokeConstructor(cl, className, permissionConstructorArgs);
+            perms.add(perm);
+        }
+        return perms.toArray(new Permission[0]);
+    }
+    
     private Object invokeStatic(ClassLoader cl, String className, String methodName,
             Object... parms) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
         Class clazz = Class.forName(className, true, cl);
@@ -403,6 +422,17 @@ public class StarterServiceDeployer {
         return method.invoke(null, parms);
     }
 
+    private Object invokeConstructor(ClassLoader cl, String className, 
+            Object... parms) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
+        Class clazz = Class.forName(className, true, cl);
+        Class[] parameterTypes = new Class[parms.length];
+        for (int i = 0; i < parms.length; i++) {
+            parameterTypes[i] = parms[i].getClass();
+        }
+        Constructor method = clazz.getConstructor(parameterTypes);
+        return method.newInstance(parms);
+    }
+
     private void grantPermissions(ClassLoader cl, Permission[] perms) {
         try {
             Class clazz = Class.forName(VirtualFileSystemConfiguration.class.getName(), true, cl);
@@ -437,7 +467,4 @@ public class StarterServiceDeployer {
         // TODO: Write this
     }
 
-    private void readMaximalPolicy() {
-        // TODO: Write this.
-    }
 }

Modified: river/jtsk/skunk/surrogate/test/org/apache/river/container/deployer/DeployerConfigParserTest.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/surrogate/test/org/apache/river/container/deployer/DeployerConfigParserTest.java?rev=1333320&r1=1333319&r2=1333320&view=diff
==============================================================================
--- river/jtsk/skunk/surrogate/test/org/apache/river/container/deployer/DeployerConfigParserTest.java (original)
+++ river/jtsk/skunk/surrogate/test/org/apache/river/container/deployer/DeployerConfigParserTest.java Thu May  3 06:21:20 2012
@@ -32,7 +32,9 @@ import static org.junit.Assert.*;
  * @author trasukg
  */
 public class DeployerConfigParserTest {
-    private static final Logger log=Logger.getLogger(DeployerConfigParserTest.class.getName());
+
+    private static final Logger log = Logger.getLogger(DeployerConfigParserTest.class.getName());
+
     public DeployerConfigParserTest() {
     }
 
@@ -54,13 +56,13 @@ public class DeployerConfigParserTest {
 
     @Test
     /**
-     Ensure that the parsing basically happens; we can create the stream and
-     run it through the parser without errors.
+    Ensure that the parsing basically happens; we can create the stream and
+    run it through the parser without errors.
      */
     public void testBasicParsing() throws ParseException {
-        ASTconfig config=parseTestConfig();
+        ASTconfig config = parseTestConfig();
         log.fine("grants string is:" + config.toString());
-        String expected="config (grant (permission java.io.FilePermission \"${serviceArchive}\" \"read\") "
+        String expected = "config (grant (permission java.io.FilePermission \"${serviceArchive}\" \"read\") "
                 + "(permission java.net.SocketPermission \"*\" \"connect\")) "
                 + "(classloader (parent systemClassLoader) "
                 + "(jars (classpath (cpEntry commons-vfs-1.0.jar) "
@@ -76,21 +78,21 @@ public class DeployerConfigParserTest {
     }
 
     private ASTconfig parseTestConfig() throws ParseException {
-        InputStream in=
+        InputStream in =
                 DeployerConfigParserTest.class.getResourceAsStream("sample.config");
-        assertTrue("No sample.config file found!", in!=null);
-        ASTconfig config=DeployerConfigParser.parseConfig(in);
+        assertTrue("No sample.config file found!", in != null);
+        ASTconfig config = DeployerConfigParser.parseConfig(in);
         return config;
     }
-    
+
     /**
     Matching the ASTConfig should return the root node.
     @throws Exception 
-    */
+     */
     @Test
     public void testPathMatch() throws Exception {
-        ASTNode config=parseTestConfig();
-        List<ASTNode> matches=config.search(new Class[]{ ASTconfig.class});
+        ASTNode config = parseTestConfig();
+        List<ASTNode> matches = config.search(new Class[]{ASTconfig.class});
         assertEquals("Length of match list", 1, matches.size());
         assertEquals("matched node", config, matches.get(0));
     }
@@ -98,14 +100,41 @@ public class DeployerConfigParserTest {
     /**
     Matching the ASTConfig should return the root node.
     @throws Exception 
-    */
+     */
     @Test
     public void testlongerPathMatch() throws Exception {
-        ASTNode config=parseTestConfig();
-        List<ASTNode> matches=config.search(
-                new Class[]{ ASTconfig.class, ASTclassloader.class,
-                ASTjars.class});
+        ASTNode config = parseTestConfig();
+        List<ASTNode> matches = config.search(
+                new Class[]{ASTconfig.class, ASTclassloader.class,
+                    ASTjars.class});
         assertEquals("Length of match list", 1, matches.size());
         assertEquals("matched node class", ASTjars.class, matches.get(0).getClass());
     }
+
+    /**
+    Checking format and contents of the permission grants.
+     */
+    @Test
+    public void testPermissionContents() throws Exception {
+        ASTNode config = parseTestConfig();
+        List<ASTNode> permNodes = config.search(
+                new Class[]{ASTconfig.class, ASTgrant.class, ASTpermission.class});
+        assertEquals("Number of permission nodes", 2, permNodes.size());
+        ASTpermission firstNode = (ASTpermission) permNodes.get(0);
+        assertEquals("permission java.io.FilePermission \"${serviceArchive}\" \"read\"", firstNode.toString());
+        assertEquals("children of permission node", 3, firstNode.jjtGetNumChildren());
+        assertEquals("Permission type for first node", "java.io.FilePermission",
+                ((ASTsymbol) (firstNode.jjtGetChild(0))).getValue());
+    }
+
+    @Test
+    public void testParentLoaderName() throws Exception {
+
+        ASTNode configNode = parseTestConfig();
+        String parentLoaderName = configNode.search(
+                new Class[]{ASTconfig.class, ASTclassloader.class, ASTparent.class}).get(0).jjtGetChild(0).toString();
+        assertEquals("parentLoaderName", "systemClassLoader", parentLoaderName);
+
+
+    }
 }
\ No newline at end of file

Modified: river/jtsk/skunk/surrogate/testfiles/testroot/profile/default/config.xml
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/surrogate/testfiles/testroot/profile/default/config.xml?rev=1333320&r1=1333319&r2=1333320&view=diff
==============================================================================
--- river/jtsk/skunk/surrogate/testfiles/testroot/profile/default/config.xml (original)
+++ river/jtsk/skunk/surrogate/testfiles/testroot/profile/default/config.xml Thu May  3 06:21:20 2012
@@ -40,14 +40,16 @@
 
     <!-- Deployer for 'service-starter'-style applications. -->
     <cfg:component class="org.apache.river.container.deployer.StarterServiceDeployer">
-        <property name="config" value="service-starter.cfg"/>
-        <property name="deployDirectory" value="deploy"/>
+        <cfg:property name="config" value="service-starter.cfg"/>
+        <cfg:property name="deployDirectory" value="deploy"/>
     </cfg:component>
     
-    <!-- Deployer for 'system apps' like the remote deployment service -->
-    <cfg:component class="org.apache.river.container.StarterServiceDeployer">
-        <property name="config" value="privileged-services.config"/>
-        <property name="deployDirectory" value="deploy-privileged"/>
+    <!-- Deployer for 'system apps' like the remote deployment service 
+    <cfg:component class="org.apache.river.container.deployer.StarterServiceDeployer">
+        <cfg:property name="config" value="privileged-services.cfg"/>
+        <cfg:property name="deployDirectory" value="deploy-privileged"/>
     </cfg:component>
+    -->
+    
     <cfg:component class="org.apache.river.container.ShowContextToConsole"/>
 </cfg:container-config>