View Javadoc

1   /*
2   Copyright (c) 2006, University of Tromsø
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without 
6   modification, are permitted provided that the following conditions are met:
7   
8    * Redistributions of source code must retain the above copyright notice, this list 
9      of conditions and the following disclaimer.
10  
11   * Redistributions in binary form must reproduce the above copyright notice, this 
12     list of conditions and the following disclaimer in the documentation and/or other 
13     materials provided with the distribution.
14  
15   * Neither the name of the University of Tromsø nor the names of its contributors may 
16     be used to endorse or promote products derived from this software without specific 
17     prior written permission.
18  
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
20  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
21  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
22  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
23  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
24  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
25  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
26  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
27  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
28  DAMAGE.
29   */
30  
31  package argos;
32  
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.IOException;
36  import java.lang.management.ManagementFactory;
37  import java.rmi.RMISecurityManager;
38  import java.util.ArrayList;
39  import java.util.List;
40  import java.util.logging.LogManager;
41  import java.util.logging.Logger;
42  
43  import argos.config.Config;
44  import argos.deploy.ComponentManager;
45  import argos.deploy.HotDeployer;
46  import argos.logging.Console;
47  import argos.naming.NamingService;
48  import argos.proxy.DynamicProxyUtil;
49  import argos.util.Util;
50  
51  public class Argos {
52      private static final Logger logger = Logger.getLogger(Argos.class.getName());
53      public static final String DEPLOY_DIR = "deploy";
54      public static final String LOG_DIR = "logs";
55      public static final String TEMP_DIR = "temp";
56      private static final String ARGOS_POLICY_FILE = "argos.policy";
57  //TODO RUn assembly:assembly on deploy. what is deploy? should it be package?
58      public static void main(String[] args) throws IOException {
59          long start = System.currentTimeMillis();
60  
61          createFolders(TEMP_DIR, LOG_DIR, DEPLOY_DIR);
62          if (!new File(Config.CONFIG_FILE).exists()) {
63              Util.writeResourceToFile("/" + Config.CONFIG_FILE, Config.CONFIG_FILE);
64          }
65          Config config = Config.getInstance();
66  //        System.setErr(Console.console);
67          LogManager.getLogManager().readConfiguration(new FileInputStream(Config.CONFIG_FILE));
68          logger.info("Log configuration file loaded.");
69  
70          String tempPolicyFile = TEMP_DIR + File.pathSeparator + ARGOS_POLICY_FILE;
71          Util.writeResourceToFile("/" + ARGOS_POLICY_FILE, tempPolicyFile);
72          System.setProperty("java.security.policy", tempPolicyFile);
73          if (System.getSecurityManager() == null) {
74              System.setSecurityManager(new RMISecurityManager());
75          }
76  
77          // Create MBean server
78          ManagementFactory.getPlatformMBeanServer();
79          logger.info("MBean server created.");
80  
81          // Instrument console
82          DynamicProxyUtil.instrument("Console", Console.console);
83  
84          // Register Config as MBean
85          DynamicProxyUtil.instrument("Config", config);
86  
87          // Component manager
88          DynamicProxyUtil.instrument("ComponentManager", ComponentManager.getInstance());
89          logger.info("Component Manager created.");
90  
91          // Naming Service
92          DynamicProxyUtil.instrument("NamingService", NamingService.getInstance());
93          logger.info("Naming service created.");
94  
95          // Hot deployer
96          if (args.length == 0) {
97              HotDeployer deployer = HotDeployer.getInstance();
98              DynamicProxyUtil.instrument("HotDeployer", deployer, false);
99              deployer.start();
100             logger.info("Hot deployer started.");
101         } else {
102             logger.info("Prefined files given on command line, not starting Hot deployer. Loading files...");
103             List<File> files = new ArrayList<File>();
104             for (String filename : args) {
105                 files.add(new File(filename));
106             }
107             ComponentManager.getInstance().deploy(files);
108         }
109 
110         Runtime.getRuntime().addShutdownHook(new ShutdownHook());
111         
112         // Done
113         double used = (System.currentTimeMillis() - start) / 1000.0;
114         logger.info("Argos core started successfully in " + used + "s.");
115     }
116 
117     private static void createFolders(String... filenames) {
118         for (String filename : filenames) {
119             File directory = new File(filename);
120             if (!directory.exists() || !directory.isDirectory()) {
121                 logger.info("Creating directory " + filename);
122                 directory.mkdir();
123             }
124         }
125     }
126 }