Author: bdekruijff at gmail.com
Date: Tue Dec 14 18:26:49 2010
New Revision: 492

Log:
[sandbox] Some basic shellcommands for testing purposes

Added:
   sandbox/bdekruijff/amdatu-shell/
   sandbox/bdekruijff/amdatu-shell/pom.xml
   sandbox/bdekruijff/amdatu-shell/tmcommands/
   sandbox/bdekruijff/amdatu-shell/tmcommands/pom.xml
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/osgi/
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/osgi/Activator.java
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/CallCommandImpl.java
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMCreateCommandImpl.java
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMDeleteCommandImpl.java
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMListCommandImpl.java
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/recources/
   
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/recources/felix.properties
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/test/
   sandbox/bdekruijff/amdatu-shell/tmcommands/src/test/java/
   sandbox/bdekruijff/amdatu-shell/uacommands/
   sandbox/bdekruijff/amdatu-shell/uacommands/pom.xml
   sandbox/bdekruijff/amdatu-shell/uacommands/src/
   sandbox/bdekruijff/amdatu-shell/uacommands/src/main/
   sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/
   sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/
   sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/
   sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/osgi/
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/osgi/Activator.java
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/CallCommandImpl.java
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UACreateCommandImpl.java
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UADeleteCommandImpl.java
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UAListCommandImpl.java
   sandbox/bdekruijff/amdatu-shell/uacommands/src/main/recources/
   
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/recources/felix.properties
   sandbox/bdekruijff/amdatu-shell/uacommands/src/test/
   sandbox/bdekruijff/amdatu-shell/uacommands/src/test/java/

Added: sandbox/bdekruijff/amdatu-shell/pom.xml
==============================================================================
--- (empty file)
+++ sandbox/bdekruijff/amdatu-shell/pom.xml     Tue Dec 14 18:26:49 2010
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu</groupId>
+    <artifactId>amdatu</artifactId>
+    <version>0.1.0-SNAPSHOT</version>
+  </parent>
+  <groupId>org.amdatu.shell</groupId>
+  <artifactId>commands</artifactId>
+  <packaging>pom</packaging>
+  <name>Amdatu Shell commands</name>
+  <description>This project provides shell commands</description>
+
+  <modules>
+    <module>tmcommands</module>
+    <module>uacommands</module>
+  </modules>
+</project>

Added: sandbox/bdekruijff/amdatu-shell/tmcommands/pom.xml
==============================================================================
--- (empty file)
+++ sandbox/bdekruijff/amdatu-shell/tmcommands/pom.xml  Tue Dec 14 18:26:49 2010
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu.shell</groupId>
+    <artifactId>commands</artifactId>
+    <version>0.1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>tmcommands</artifactId>
+  <packaging>bundle</packaging>
+  <name>Amdatu Tenant Manager Commands</name>
+  <description></description>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.shell</artifactId>
+      <type>bundle</type>
+      <scope>provided</scope>
+      <version>1.4.2</version>
+    </dependency>
+    <dependency>
+      <groupId>org.amdatu.core</groupId>
+      <artifactId>tenant</artifactId>
+      <scope>provided</scope>
+      <type>bundle</type>
+      <version>0.1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.amdatu.tm</groupId>
+      <artifactId>tenantmanager</artifactId>
+      <scope>provided</scope>
+      <type>bundle</type>
+      <version>0.1.0-SNAPSHOT</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <configuration>
+          <instructions>
+            
<Bundle-Activator>org.amdatu.shell.tmcommands.osgi.Activator</Bundle-Activator>
+            
<Bundle-SymbolicName>org.amdatu.shell.tmcommands</Bundle-SymbolicName>
+            <Embed-Dependency>*;scope=compile;inline=true</Embed-Dependency>
+            <Embed-Transitive>true</Embed-Transitive>
+            <Bundle-ClassPath>.</Bundle-ClassPath>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Added: 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/osgi/Activator.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/osgi/Activator.java
    Tue Dec 14 18:26:49 2010
@@ -0,0 +1,69 @@
+/*
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.shell.tmcommands.osgi;
+
+import org.amdatu.core.tenant.TenantManagementService;
+import org.amdatu.shell.tmcommands.service.CallCommandImpl;
+import org.amdatu.shell.tmcommands.service.TMCreateCommandImpl;
+import org.amdatu.shell.tmcommands.service.TMDeleteCommandImpl;
+import org.amdatu.shell.tmcommands.service.TMListCommandImpl;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.shell.Command;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+/**
+ */
+public final class Activator extends DependencyActivatorBase {
+
+    private BundleContext m_context;
+
+    @Override
+    public void init(BundleContext context, DependencyManager manager) throws 
Exception {
+
+        manager.add(
+            createComponent()
+                .setImplementation(CallCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(LogService.class).setRequired(false)));
+
+        manager.add(
+            createComponent()
+                .setImplementation(TMCreateCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(TenantManagementService.class).setRequired(true)));
+
+        manager.add(
+            createComponent()
+                .setImplementation(TMDeleteCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(TenantManagementService.class).setRequired(true)));
+
+        manager.add(
+            createComponent()
+                .setImplementation(TMListCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(TenantManagementService.class).setRequired(true)));
+
+    }
+
+    @Override
+    public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
+    }
+}

Added: 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/CallCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/CallCommandImpl.java
   Tue Dec 14 18:26:49 2010
@@ -0,0 +1,173 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.shell.tmcommands.service;
+
+import java.io.PrintStream;
+import java.lang.reflect.Method;
+import java.util.StringTokenizer;
+
+import org.apache.felix.shell.Command;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+public class CallCommandImpl implements Command {
+
+    private BundleContext bundleContext;
+
+    public String getName() {
+        return "wmcall";
+    }
+
+    public String getUsage() {
+        return "wmcall <interface> <method> [arg1[,arg2[...]]]";
+    }
+
+    public String getShortDescription() {
+        return "Call a method on a registered servcie";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        String serviceName = null;
+        String methodName = null;
+        Object[] args = null;
+
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken(); // ignore command itself
+
+        if (st.hasMoreTokens()) {
+            serviceName = st.nextToken();
+            if (st.hasMoreTokens()) {
+                methodName = st.nextToken();
+            }
+            int count = st.countTokens();
+            args = new Object[count];
+            if (st.hasMoreTokens()) {
+                for (int i = 0; i < count; i++) {
+                    args[i] = st.nextToken();
+                }
+            }
+        }
+
+        // Exit on parse problem
+        if (serviceName == null || methodName == null) {
+            out.println(getUsage());
+            return;
+        }
+
+        try {
+
+            // Locate the service
+            Object serviceObject = null;
+
+            ServiceReference[] refs = bundleContext.getServiceReferences(
+                    serviceName, null);
+            if (refs != null && refs.length > 0) {
+                serviceObject = bundleContext.getService(refs[0]);
+            }
+
+            // Exit on no such service
+            if (serviceObject == null) {
+                out.println("Unable to locate service (" + serviceName + ")");
+                return;
+            }
+
+            // Build argtypearray (strings only for now)
+            Class[] argTypes = new Class[args.length];
+            for (int i = 0; i < args.length; i++) {
+                argTypes[i] = String.class;
+            }
+
+            // Get the class
+            Class serviceClass = serviceObject.getClass();
+
+            // Get the method
+            Method methodObject = null;
+
+            try {
+                methodObject = serviceClass.getMethod(methodName, argTypes);
+            }
+            catch (Exception e1) {
+
+                // Try with ints
+                boolean intFound = false;
+                for (int i = 0; i < args.length; i++) {
+                    try {
+                        int argInt = Integer.parseInt((String) args[i]);
+
+                        intFound = true;
+                        args[i] = argInt;
+                        argTypes[i] = int.class;
+                    }
+                    catch (NumberFormatException nfe) {
+                        // not a number
+                    }
+                }
+
+                // Get the method
+                if (intFound) {
+                    try {
+                        methodObject = serviceClass.getMethod(methodName, 
argTypes);
+                    }
+                    catch (Exception e2) {
+                        // still not working only for strings and ints
+                    }
+                }
+
+                if (methodObject == null) {
+                    // Try with booleans
+                    boolean booleanFound = false;
+                    for (int i = 0; i < args.length; i++) {
+                        String arg = (String) args[i];
+                        if (arg.equalsIgnoreCase("true") || 
arg.equalsIgnoreCase("false")) {
+                            booleanFound = true;
+
+                            args[i] = arg.equalsIgnoreCase("true");
+                            argTypes[i] = boolean.class;
+                        }
+                    }
+
+                    if (booleanFound) {
+                        try {
+                            methodObject = serviceClass.getMethod(methodName, 
argTypes);
+                        }
+                        catch (Exception e3) {
+                            // not found
+                            out
+                                .println("Unable to locate method with String 
and/or int and/or boolean signature");
+
+                            return;
+                        }
+                    }
+                }
+            }
+
+            // Invoke the method
+            Object result = methodObject.invoke(serviceObject, args);
+            if (result != null) {
+                out.println(result.toString());
+            }
+
+        }
+        catch (Exception e) {
+            out.println("Something went wrong :S");
+            e.printStackTrace(out);
+        }
+    }
+
+}

Added: 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMCreateCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMCreateCommandImpl.java
       Tue Dec 14 18:26:49 2010
@@ -0,0 +1,66 @@
+package org.amdatu.shell.tmcommands.service;
+
+import java.io.PrintStream;
+import java.util.StringTokenizer;
+
+import org.amdatu.core.tenant.Tenant;
+import org.amdatu.core.tenant.TenantException;
+import org.amdatu.core.tenant.TenantManagementService;
+import org.apache.felix.shell.Command;
+
+public class TMCreateCommandImpl implements Command {
+
+    private volatile TenantManagementService m_tms;
+
+    public String getName() {
+        return "tmcreate";
+    }
+
+    public String getUsage() {
+        return "tmcreate <tenantId> <tenantName>";
+    }
+
+    public String getShortDescription() {
+        return "Create a tenant";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        Object[] args = null;
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken();
+        if (!st.hasMoreTokens() || st.countTokens() != 2) {
+            out.println(getUsage());
+            return;
+        }
+        String id = st.nextToken();
+        String name = st.nextToken();
+
+        if (id.equals("stresstest")) {
+            for (int i = 6000; i < 7000; i++) {
+                createTenant("" + i, name + "_" + i, out, err);
+            }
+        }
+        else {
+            createTenant(id, name, out, err);
+        }
+    }
+
+    private void createTenant(String id, String name, PrintStream out, 
PrintStream err) {
+        try {
+            Tenant t = m_tms.getTenantById(id);
+            if (t != null) {
+                out.println("Tenant allready exists: " + id);
+            }
+            t = m_tms.createTenant(id, name);
+            if (t != null) {
+                out.println("Tenant created: " + name + " (" + id + ")");
+            }
+        }
+        catch (TenantException e) {
+            err.println("Failed to execute command: " + e.getMessage());
+        }
+
+    }
+}

Added: 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMDeleteCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMDeleteCommandImpl.java
       Tue Dec 14 18:26:49 2010
@@ -0,0 +1,71 @@
+package org.amdatu.shell.tmcommands.service;
+
+import java.io.PrintStream;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.amdatu.core.tenant.TenantEntity;
+import org.amdatu.core.tenant.TenantException;
+import org.amdatu.core.tenant.TenantManagementService;
+import org.apache.felix.shell.Command;
+
+public class TMDeleteCommandImpl implements Command {
+
+    private volatile TenantManagementService m_tms;
+
+    public String getName() {
+        return "tmdelete";
+    }
+
+    public String getUsage() {
+        return "tmdelete <tenantId>";
+    }
+
+    public String getShortDescription() {
+        return "Delete a tenant";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        Object[] args = null;
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken();
+        if (!st.hasMoreTokens() || st.countTokens() != 1) {
+            out.println(getUsage());
+            return;
+        }
+        String id = st.nextToken();
+
+        if (id.equals("all")) {
+            try {
+                List<TenantEntity> ts = m_tms.getTenants();
+                for (TenantEntity t : ts) {
+                    deleteTenant(t.getId(), out, err);
+                }
+            }
+            catch (TenantException e) {
+                e.printStackTrace();
+            }
+        }
+        else {
+            deleteTenant(id, out, err);
+        }
+    }
+
+    private void deleteTenant(String id, PrintStream out, PrintStream err) {
+        try {
+            TenantEntity t = m_tms.getTenantById(id);
+            if (t == null) {
+                out.println("Tenant does not exist: '" + id + "'");
+            }
+            else {
+                m_tms.deleteTenant(t);
+                out.println("Tenant deleted: " + id);
+            }
+        }
+        catch (TenantException e) {
+            err.println("Failed to execute command: " + e.getMessage());
+        }
+    }
+}
\ No newline at end of file

Added: 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMListCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/java/org/amdatu/shell/tmcommands/service/TMListCommandImpl.java
 Tue Dec 14 18:26:49 2010
@@ -0,0 +1,45 @@
+package org.amdatu.shell.tmcommands.service;
+
+import java.io.PrintStream;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.amdatu.core.tenant.TenantEntity;
+import org.amdatu.core.tenant.TenantException;
+import org.amdatu.core.tenant.TenantManagementService;
+import org.apache.felix.shell.Command;
+
+public class TMListCommandImpl implements Command {
+
+    private volatile TenantManagementService m_tms;
+
+    public String getName() {
+        return "tmlist";
+    }
+
+    public String getUsage() {
+        return "tmlist";
+    }
+
+    public String getShortDescription() {
+        return "List tenants";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        Object[] args = null;
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken();
+
+        try {
+            List<TenantEntity> t = m_tms.getTenants();
+            for (TenantEntity te : t) {
+                out.println("'" + te.getId() + "'\t- " + te.getName());
+            }
+        }
+        catch (TenantException e) {
+            err.println("Failed to execute command: " + e.getMessage());
+        }
+    }
+}

Added: 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/recources/felix.properties
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/tmcommands/src/main/recources/felix.properties  
    Tue Dec 14 18:26:49 2010
@@ -0,0 +1,61 @@
+#org.osgi.framework.system.packages=
+org.osgi.framework.system.packages.extra=sun.misc,com.sun.management
+org.osgi.framework.startlevel.beginning=30
+org.osgi.framework.storage=felix-deploy
+felix.startlevel.bundle=30
+felix.cache.rootdir=work/cache
+
+
+#org.osgi.framework.bootdelegation=sun.*,com.sun.*
+#felix.bootdelegation.implicit=false
+#org.osgi.framework.storage.clean=onFirstInit
+#felix.auto.deploy.action=install,start,update,uninstall
+#felix.auto.deploy.dir=amdatu-system
+#felix.auto.install.5=
+
+
+# Sets the start level of newly installed bundles.
+
+# Felix installs a stream and content handler factories by default,
+# uncomment the following line to not install them.
+#felix.service.urlhandlers=false
+# The launcher registers a shutdown hook to cleanly stop the framework
+# by default, uncomment the following line to disable it.
+#felix.shutdown.hook=false
+# Config of file install
+#felix.fileinstall.poll=3000
+#felix.fileinstall.dir=deploy
+#felix.fileinstall.debug=1
+#felix.fileinstall.bundles.new.start=true
+#felix.fileinstall.filter=.*
+#felix.fileinstall.tmpdir=work/tmp/web/org/apache/felix/fileinstall
+#felix.fileinstall.start.level=30
+#Config of config admin
+#felix.cm.dir=${user.dir}/work/configadmin
+#felix.auto.start.1=reference:file:amdatu-system/org.apache.felix.configadmin-1.2.4.jar
 \
+#                                      
reference:file:amdatu-system/org.apache.felix.dependencymanager-3.0.0-SNAPSHOT.jar
 \
+#                                      
reference:file:amdatu-system/org.apache.felix.eventadmin-1.2.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.fileinstall-3.0.0.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.log-1.0.0.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.main-2.0.5.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.metatype-1.0.4.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.shell.tui-1.4.1.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.shell-1.4.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.webconsole-3.1.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.sling.commons.mime-2.1.4.jar \
+#                                      
reference:file:amdatu-system/org.apache.sling.commons.osgi-2.0.6.jar \
+#                                      
reference:file:amdatu-system/org.osgi.compendium-4.2.0.jar \
+#                                      
reference:file:amdatu-system/org.osgi.core-4.2.0.jar \
+#                                      
reference:file:amdatu-system/pax-useradmin-service-0.0.1-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.http.jetty-2.0.4.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.http.whiteboard-2.0.4.jar \
+#                                      
reference:file:amdatu-system/pax-swissbox-core-1.3.0.jar \
+#                                      
reference:file:amdatu-system/ops4j-base-lang-1.2.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.scr-1.6.0.jar
+#felix.auto.start.2=reference:file:amdatu-core/org.amdatu.core.config.filebased-0.1.0-SNAPSHOT.jar
 \
+#                                      
reference:file:amdatu-core/org.amdatu.core.loghandler-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.tenant-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.tenantstore-fs-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.config.templates-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.useradminstore-fs-0.1.0-SNAPSHOT.jar 
+#
\ No newline at end of file

Added: sandbox/bdekruijff/amdatu-shell/uacommands/pom.xml
==============================================================================
--- (empty file)
+++ sandbox/bdekruijff/amdatu-shell/uacommands/pom.xml  Tue Dec 14 18:26:49 2010
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu.shell</groupId>
+    <artifactId>commands</artifactId>
+    <version>0.1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>aucommands</artifactId>
+  <packaging>bundle</packaging>
+  <name>Amdatu UserAdmin Commands</name>
+  <description></description>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.shell</artifactId>
+      <type>bundle</type>
+      <scope>provided</scope>
+      <version>1.4.2</version>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.useradmin</groupId>
+      <artifactId>pax-useradmin-service</artifactId>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <configuration>
+          <instructions>
+            
<Bundle-Activator>org.amdatu.shell.uacommands.osgi.Activator</Bundle-Activator>
+            
<Bundle-SymbolicName>org.amdatu.shell.uacommands</Bundle-SymbolicName>
+            <Bundle-ClassPath>.</Bundle-ClassPath>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Added: 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/osgi/Activator.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/osgi/Activator.java
    Tue Dec 14 18:26:49 2010
@@ -0,0 +1,69 @@
+/*
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.shell.uacommands.osgi;
+
+import org.amdatu.shell.uacommands.service.CallCommandImpl;
+import org.amdatu.shell.uacommands.service.UACreateCommandImpl;
+import org.amdatu.shell.uacommands.service.UADeleteCommandImpl;
+import org.amdatu.shell.uacommands.service.UAListCommandImpl;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.shell.Command;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+import org.osgi.service.useradmin.UserAdmin;
+
+/**
+ */
+public final class Activator extends DependencyActivatorBase {
+
+    private BundleContext m_context;
+
+    @Override
+    public void init(BundleContext context, DependencyManager manager) throws 
Exception {
+
+        manager.add(
+            createComponent()
+                .setImplementation(CallCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(LogService.class).setRequired(false)));
+
+        manager.add(
+            createComponent()
+                .setImplementation(UACreateCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(UserAdmin.class).setRequired(true)));
+
+        manager.add(
+            createComponent()
+                .setImplementation(UADeleteCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(UserAdmin.class).setRequired(true)));
+
+        manager.add(
+            createComponent()
+                .setImplementation(UAListCommandImpl.class)
+                .setInterface(Command.class.getName(), null)
+                
.add(createServiceDependency().setService(UserAdmin.class).setRequired(true)));
+
+    }
+
+    @Override
+    public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
+    }
+}

Added: 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/CallCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/CallCommandImpl.java
   Tue Dec 14 18:26:49 2010
@@ -0,0 +1,173 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.shell.uacommands.service;
+
+import java.io.PrintStream;
+import java.lang.reflect.Method;
+import java.util.StringTokenizer;
+
+import org.apache.felix.shell.Command;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+public class CallCommandImpl implements Command {
+
+    private BundleContext bundleContext;
+
+    public String getName() {
+        return "wmcall";
+    }
+
+    public String getUsage() {
+        return "wmcall <interface> <method> [arg1[,arg2[...]]]";
+    }
+
+    public String getShortDescription() {
+        return "Call a method on a registered servcie";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        String serviceName = null;
+        String methodName = null;
+        Object[] args = null;
+
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken(); // ignore command itself
+
+        if (st.hasMoreTokens()) {
+            serviceName = st.nextToken();
+            if (st.hasMoreTokens()) {
+                methodName = st.nextToken();
+            }
+            int count = st.countTokens();
+            args = new Object[count];
+            if (st.hasMoreTokens()) {
+                for (int i = 0; i < count; i++) {
+                    args[i] = st.nextToken();
+                }
+            }
+        }
+
+        // Exit on parse problem
+        if (serviceName == null || methodName == null) {
+            out.println(getUsage());
+            return;
+        }
+
+        try {
+
+            // Locate the service
+            Object serviceObject = null;
+
+            ServiceReference[] refs = bundleContext.getServiceReferences(
+                    serviceName, null);
+            if (refs != null && refs.length > 0) {
+                serviceObject = bundleContext.getService(refs[0]);
+            }
+
+            // Exit on no such service
+            if (serviceObject == null) {
+                out.println("Unable to locate service (" + serviceName + ")");
+                return;
+            }
+
+            // Build argtypearray (strings only for now)
+            Class[] argTypes = new Class[args.length];
+            for (int i = 0; i < args.length; i++) {
+                argTypes[i] = String.class;
+            }
+
+            // Get the class
+            Class serviceClass = serviceObject.getClass();
+
+            // Get the method
+            Method methodObject = null;
+
+            try {
+                methodObject = serviceClass.getMethod(methodName, argTypes);
+            }
+            catch (Exception e1) {
+
+                // Try with ints
+                boolean intFound = false;
+                for (int i = 0; i < args.length; i++) {
+                    try {
+                        int argInt = Integer.parseInt((String) args[i]);
+
+                        intFound = true;
+                        args[i] = argInt;
+                        argTypes[i] = int.class;
+                    }
+                    catch (NumberFormatException nfe) {
+                        // not a number
+                    }
+                }
+
+                // Get the method
+                if (intFound) {
+                    try {
+                        methodObject = serviceClass.getMethod(methodName, 
argTypes);
+                    }
+                    catch (Exception e2) {
+                        // still not working only for strings and ints
+                    }
+                }
+
+                if (methodObject == null) {
+                    // Try with booleans
+                    boolean booleanFound = false;
+                    for (int i = 0; i < args.length; i++) {
+                        String arg = (String) args[i];
+                        if (arg.equalsIgnoreCase("true") || 
arg.equalsIgnoreCase("false")) {
+                            booleanFound = true;
+
+                            args[i] = arg.equalsIgnoreCase("true");
+                            argTypes[i] = boolean.class;
+                        }
+                    }
+
+                    if (booleanFound) {
+                        try {
+                            methodObject = serviceClass.getMethod(methodName, 
argTypes);
+                        }
+                        catch (Exception e3) {
+                            // not found
+                            out
+                                .println("Unable to locate method with String 
and/or int and/or boolean signature");
+
+                            return;
+                        }
+                    }
+                }
+            }
+
+            // Invoke the method
+            Object result = methodObject.invoke(serviceObject, args);
+            if (result != null) {
+                out.println(result.toString());
+            }
+
+        }
+        catch (Exception e) {
+            out.println("Something went wrong :S");
+            e.printStackTrace(out);
+        }
+    }
+
+}

Added: 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UACreateCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UACreateCommandImpl.java
       Tue Dec 14 18:26:49 2010
@@ -0,0 +1,47 @@
+package org.amdatu.shell.uacommands.service;
+
+import java.io.PrintStream;
+import java.util.StringTokenizer;
+
+import org.apache.felix.shell.Command;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.UserAdmin;
+
+public class UACreateCommandImpl implements Command {
+
+    private volatile UserAdmin m_ua;
+
+    public String getName() {
+        return "uacreate";
+    }
+
+    public String getUsage() {
+        return "uacreate <userId>";
+    }
+
+    public String getShortDescription() {
+        return "Create a user";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        Object[] args = null;
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken();
+        if (!st.hasMoreTokens() || st.countTokens() != 1) {
+            out.println(getUsage());
+            return;
+        }
+        String id = st.nextToken();
+
+        Role r = m_ua.getRole(id);
+        if (r != null) {
+            out.println("User allready exists: " + id);
+        }
+        r = m_ua.createRole(id, Role.USER);
+        if (r != null) {
+            out.println("User created: " + id);
+        }
+    }
+}

Added: 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UADeleteCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UADeleteCommandImpl.java
       Tue Dec 14 18:26:49 2010
@@ -0,0 +1,47 @@
+package org.amdatu.shell.uacommands.service;
+
+import java.io.PrintStream;
+import java.util.StringTokenizer;
+
+import org.apache.felix.shell.Command;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.UserAdmin;
+
+public class UADeleteCommandImpl implements Command {
+
+    private volatile UserAdmin m_ua;
+
+    public String getName() {
+        return "uadelete";
+    }
+
+    public String getUsage() {
+        return "uadelete <userId>";
+    }
+
+    public String getShortDescription() {
+        return "Delete a user";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        Object[] args = null;
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken();
+        if (!st.hasMoreTokens() || st.countTokens() != 1) {
+            out.println(getUsage());
+            return;
+        }
+        String id = st.nextToken();
+
+        Role r = m_ua.getRole(id);
+        if (r == null) {
+            out.println("User does not exist: '" + id + "'");
+        }
+        else {
+            m_ua.removeRole(id);
+            out.println("User deleted: " + id);
+        }
+    }
+}
\ No newline at end of file

Added: 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UAListCommandImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/java/org/amdatu/shell/uacommands/service/UAListCommandImpl.java
 Tue Dec 14 18:26:49 2010
@@ -0,0 +1,46 @@
+package org.amdatu.shell.uacommands.service;
+
+import java.io.PrintStream;
+import java.util.StringTokenizer;
+
+import org.apache.felix.shell.Command;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.UserAdmin;
+
+public class UAListCommandImpl implements Command {
+
+    private volatile UserAdmin m_ua;
+
+    public String getName() {
+        return "ualist";
+    }
+
+    public String getUsage() {
+        return "ualist";
+    }
+
+    public String getShortDescription() {
+        return "List users";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err) {
+
+        // Parse command line.
+        Object[] args = null;
+        StringTokenizer st = new StringTokenizer(s, " ");
+        st.nextToken();
+
+        Role[] rs;
+        try {
+            rs = m_ua.getRoles(null);
+            for (Role r : rs) {
+                out.println("" + r.getName());
+            }
+        }
+        catch (InvalidSyntaxException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+}

Added: 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/recources/felix.properties
==============================================================================
--- (empty file)
+++ 
sandbox/bdekruijff/amdatu-shell/uacommands/src/main/recources/felix.properties  
    Tue Dec 14 18:26:49 2010
@@ -0,0 +1,61 @@
+#org.osgi.framework.system.packages=
+org.osgi.framework.system.packages.extra=sun.misc,com.sun.management
+org.osgi.framework.startlevel.beginning=30
+org.osgi.framework.storage=felix-deploy
+felix.startlevel.bundle=30
+felix.cache.rootdir=work/cache
+
+
+#org.osgi.framework.bootdelegation=sun.*,com.sun.*
+#felix.bootdelegation.implicit=false
+#org.osgi.framework.storage.clean=onFirstInit
+#felix.auto.deploy.action=install,start,update,uninstall
+#felix.auto.deploy.dir=amdatu-system
+#felix.auto.install.5=
+
+
+# Sets the start level of newly installed bundles.
+
+# Felix installs a stream and content handler factories by default,
+# uncomment the following line to not install them.
+#felix.service.urlhandlers=false
+# The launcher registers a shutdown hook to cleanly stop the framework
+# by default, uncomment the following line to disable it.
+#felix.shutdown.hook=false
+# Config of file install
+#felix.fileinstall.poll=3000
+#felix.fileinstall.dir=deploy
+#felix.fileinstall.debug=1
+#felix.fileinstall.bundles.new.start=true
+#felix.fileinstall.filter=.*
+#felix.fileinstall.tmpdir=work/tmp/web/org/apache/felix/fileinstall
+#felix.fileinstall.start.level=30
+#Config of config admin
+#felix.cm.dir=${user.dir}/work/configadmin
+#felix.auto.start.1=reference:file:amdatu-system/org.apache.felix.configadmin-1.2.4.jar
 \
+#                                      
reference:file:amdatu-system/org.apache.felix.dependencymanager-3.0.0-SNAPSHOT.jar
 \
+#                                      
reference:file:amdatu-system/org.apache.felix.eventadmin-1.2.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.fileinstall-3.0.0.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.log-1.0.0.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.main-2.0.5.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.metatype-1.0.4.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.shell.tui-1.4.1.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.shell-1.4.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.webconsole-3.1.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.sling.commons.mime-2.1.4.jar \
+#                                      
reference:file:amdatu-system/org.apache.sling.commons.osgi-2.0.6.jar \
+#                                      
reference:file:amdatu-system/org.osgi.compendium-4.2.0.jar \
+#                                      
reference:file:amdatu-system/org.osgi.core-4.2.0.jar \
+#                                      
reference:file:amdatu-system/pax-useradmin-service-0.0.1-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.http.jetty-2.0.4.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.http.whiteboard-2.0.4.jar \
+#                                      
reference:file:amdatu-system/pax-swissbox-core-1.3.0.jar \
+#                                      
reference:file:amdatu-system/ops4j-base-lang-1.2.2.jar \
+#                                      
reference:file:amdatu-system/org.apache.felix.scr-1.6.0.jar
+#felix.auto.start.2=reference:file:amdatu-core/org.amdatu.core.config.filebased-0.1.0-SNAPSHOT.jar
 \
+#                                      
reference:file:amdatu-core/org.amdatu.core.loghandler-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.tenant-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.tenantstore-fs-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.config.templates-0.1.0-SNAPSHOT.jar \
+#                                      
reference:file:amdatu-core/org.amdatu.core.useradminstore-fs-0.1.0-SNAPSHOT.jar 
+#
\ No newline at end of file

Reply via email to