com/scalagent/joram/osgi/server/Activator.java

00001 package com.scalagent.joram.osgi.server;
00002 
00003 import java.io.*;
00004 import java.util.*;
00005 
00006 import org.osgi.framework.*;
00007 
00008 import com.scalagent.joram.osgi.server.service.JoramAdmin;
00009 
00010 import org.objectweb.joram.client.jms.ConnectionFactory;
00011 import org.objectweb.joram.client.jms.local.LocalConnectionFactory;
00012 import org.objectweb.joram.client.jms.admin.AdminModule;
00013 import org.objectweb.joram.client.jms.admin.User;
00014 import org.objectweb.joram.client.jms.Queue;
00015 import org.objectweb.joram.client.jms.Topic;
00016 
00017 import fr.dyade.aaa.agent.AgentServer;
00018 import fr.dyade.aaa.agent.*;
00019 
00020 public class Activator implements BundleActivator {
00021 
00022   private static BundleContext bcontext;
00023 
00024   public static BundleContext getBundleContext() {
00025     return bcontext;
00026   }
00027 
00034   public void start(BundleContext context) throws Exception {
00035     bcontext = context;
00036 
00037     startAgentServer();
00038 
00039     Properties props = new Properties();
00040     context.registerService(JoramAdmin.class.getName(), 
00041                             new JoramAdminImpl(),
00042                             props);
00043   }
00044 
00045   private void startAgentServer() throws Exception {
00046     //search a3server.prop in path used to load classes.
00047     ClassLoader classLoader = getClass().getClassLoader();
00048     InputStream is = classLoader.getResourceAsStream("a3server.prop");
00049     Properties a3prop = new Properties();
00050     a3prop.load(is);
00051 
00052     String serverId = a3prop.getProperty("sid");
00053     String path = a3prop.getProperty("path");
00054     String relativePath = a3prop.getProperty("relativePath");
00055     String storageDirName = a3prop.getProperty("storageDirName");
00056     String logicalServerName = a3prop.getProperty("logicalServerName");
00057 
00058     File storageDir = new File(new File(path), storageDirName);
00059     File lockFile = new File(storageDir, "lock");
00060     lockFile.delete();
00061 
00062     // find the most precise debug file
00063     if (logicalServerName != null) try {
00064       String debugDir = System.getProperty(
00065         Debug.DEBUG_DIR_PROPERTY, Debug.DEFAULT_DEBUG_DIR);
00066       File debugFile = new File(debugDir, logicalServerName);
00067       if ((debugFile != null) &&
00068           debugFile.exists() &&
00069           debugFile.isDirectory()) {
00070         debugDir = debugFile.getPath();
00071         String debugFileName = System.getProperty(
00072           Debug.DEBUG_FILE_PROPERTY, Debug.DEFAULT_DEBUG_FILE);
00073         debugFile = new File(debugFile, debugFileName);
00074         if ((debugFile != null) &&
00075             debugFile.exists() &&
00076             debugFile.isFile() &&
00077             (debugFile.length() != 0)) {
00078           Debug.setDebugDir(debugDir);
00079           Debug.setDebugFileName(debugFileName);
00080         }
00081       }
00082     } catch (Exception exc) {
00083     }
00084 
00085     AgentServer.init(new String[] {
00086       serverId, 
00087       storageDir.getAbsolutePath()});
00088     AgentServer.start();
00089 
00090   }
00091 
00097   public void stop(BundleContext context) {
00098     stopAgentServer();
00099   }
00100 
00101   private void stopAgentServer()  {
00102     AgentServer.stop();
00103     AgentServer.reset();
00104   }
00105 
00106   public static class JoramAdminImpl implements JoramAdmin {
00107 
00108     private String rootUserName= "root";
00109 
00110     public void setRootUserName(String root) {
00111       this.rootUserName = root;
00112     }
00113 
00114     private String rootPassword = "root";
00115 
00116     public void setRootPassword(String password) {
00117       this.rootPassword = password;
00118     }
00119 
00123     public ConnectionFactory getLocalConnectionFactory() throws Exception {
00124       return (ConnectionFactory) LocalConnectionFactory.create();
00125     }
00126 
00130     public boolean executeAdminXML(Reader reader) throws Exception {
00131       return AdminModule.executeAdmin(reader);
00132     }
00133 
00139     public void createUser(String name, String password) throws Exception {
00140       try {
00141         AdminModule.collocatedConnect(rootUserName, rootPassword);
00142         User.create(name, password);
00143       } finally {
00144         AdminModule.disconnect();
00145       }
00146     }
00147 
00156     public Queue createQueue(String name) throws Exception {
00157       Queue queue = null;
00158       try {
00159         AdminModule.collocatedConnect(rootUserName, rootPassword);
00160         queue = Queue.create(name);
00161         queue.setFreelyWriteable(true);
00162         queue.setFreelyReadable(true);
00163       } finally {
00164         AdminModule.disconnect();
00165       }
00166       return queue;
00167     }
00168 
00175     public Topic createTopic(String name) throws Exception {
00176       Topic topic = null;
00177       try {
00178         AdminModule.collocatedConnect(rootUserName, rootPassword);
00179         topic = Topic.create(name);
00180         topic.setFreelyWriteable(true);
00181         topic.setFreelyReadable(true);
00182       } finally {
00183         AdminModule.disconnect();
00184       }
00185       return topic;
00186     }
00187 
00191     public List getDestinations() throws Exception {
00192       List destinations = null;;
00193       try {
00194         AdminModule.collocatedConnect(rootUserName, rootPassword);
00195         destinations = AdminModule.getDestinations();
00196       } finally {
00197         AdminModule.disconnect();
00198       }
00199       return destinations;
00200     }
00201 
00205     public List getUsers() throws Exception {
00206       List users = null;;
00207       try {
00208         AdminModule.collocatedConnect(rootUserName, rootPassword);
00209         users = AdminModule.getUsers();
00210       } finally {
00211         AdminModule.disconnect();
00212       }
00213       return users;
00214     }
00215   }
00216 }

Generated on Tue Sep 16 16:14:23 2008 for joram by  doxygen 1.5.0