summaryrefslogtreecommitdiffstats
blob: 20b909aafbf3c382da15a3762d7171afee38449d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jini.utility;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.jini.core.entry.Entry;
import net.jini.lookup.entry.Comment;
import net.jini.lookup.entry.ServiceInfo;
import org.eclipse.osee.framework.jdk.core.util.CmdLineArgs;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.OseeProperties;
import org.eclipse.osee.framework.jini.discovery.ServiceDataStore;
import org.eclipse.osee.framework.jini.service.core.JiniService;
import org.eclipse.osee.framework.jini.service.core.SimpleFormattedEntry;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.config.JiniLookupGroupConfig;
import org.eclipse.osee.framework.plugin.core.config.NonEclipseManifestHeader;

/**
 * @author Andrew M. Finkbeiner
 */
public class StartJini extends JiniService {

   public static final String SPAWNED_REGGIE_SERVICE_ID = "Spawned Reggie Id";
   public static final String SPAWNED_REGGIE_ON_HOST = "On Host";

   private final List<Process> jiniProcesses;

   public StartJini(String port, boolean nohup, boolean browser, String jiniHome, InputStream manifestFile) {
      super();

      jiniProcesses = new ArrayList<Process>();

      try {
         String javaHome = System.getProperty("java.home");
         String fs = System.getProperty("file.separator");

         if (jiniHome == null) {
            jiniHome = new File(Lib.getBasePath(StartJini.class).replace("bin", "")).getAbsolutePath();
         }
         jiniHome = jiniHome.replace('\\', '/');

         String[] groups = JiniLookupGroupConfig.getOseeJiniServiceGroups();
         String allowedGroups = "";
         if (groups != null) {
            allowedGroups = Arrays.toString(groups);
         } else {
            OseeLog.log(
               StartJini.class,
               Level.SEVERE,
               "[-D" + OseeProperties.getOseeJiniServiceGroups() + "] was not set.\nPlease enter the Group(s) this Lookup Server will register with.");
            return;
         }
         String quote = null;
         if (Lib.isWindows()) {
            quote = "\"";
         } else {
            quote = "";
         }

         String host = InetAddress.getLocalHost().getHostAddress();
         System.out.println("Host Address: " + host);
         String javaexeBigMem = quote + javaHome + fs + "bin" + fs + "java" + quote + " -Xmx512M ";
         String startServices =
            javaexeBigMem + " -Dlookupcomponent " + " -Dosee.jini.lookup.groups=" + allowedGroups + " " + " -Dosee.jini.install=" + quote + jiniHome + "/jini2_1" + quote + " " + " -Dosee.jini.config=" + quote + jiniHome + "/jini_config" + quote + " " + " -Dosee.classserver.host=" + host + " " + " -Dosee.classserver.port=" + port + " " + " -Djava.security.policy=" + quote + jiniHome + "/jini_config/jsk-all.policy" + quote + " " + " -jar " + quote + jiniHome + "/jini2_1/lib/start.jar" + quote + " " + quote + jiniHome + "/jini_config/start-transient-jeri-services.config" + quote;

         OseeLog.log(StartJini.class, Level.INFO, "RUN REGGIE ***************************************************");
         OseeLog.log(StartJini.class, Level.INFO, startServices);
         Process process = Runtime.getRuntime().exec(startServices);
         jiniProcesses.add(process);
         String reggieServiceId = catchTheReggieServiceId(process);
         Lib.handleProcessNoWait(process, new OutputStreamWriter(System.err));

         if (reggieServiceId == null || reggieServiceId.length() == 0) {
            OseeLog.log(StartJini.class, Level.SEVERE, "\n Jini Initialization Failed. \n");
            killProcesses();
            return;
         }

         // Wait for Reggie to come alive before registering Core Jini Service
         try {
            Thread.sleep(7000);
         } catch (InterruptedException ex) {
            ex.printStackTrace();
         }

         String model = "CJS";
         String description = "Provides means to shutdown spawned Jini Lookup Services";

         this.registerService(new Entry[] {
            new ServiceInfo("", "", "", "", model, ""),
            new Comment(description),
            new SimpleFormattedEntry(SPAWNED_REGGIE_SERVICE_ID, reggieServiceId),
            new SimpleFormattedEntry(SPAWNED_REGGIE_ON_HOST, getHostName())}, getHeaders(manifestFile));

         OseeLog.log(StartJini.class, Level.INFO, "....................Core Jini Service is Alive....................");
         this.stayAlive();

      } catch (IOException ex) {
         ex.printStackTrace();
      }

   }

   private String catchTheReggieServiceId(Process process) {
      Pattern reggieStartPattern = Pattern.compile("INFO: started Reggie: ([a-z|A-Z|0-9|\\-]+).*");
      String reggieFail = "SEVERE: Reggie initialization failed";
      StringBuilder wr = new StringBuilder();
      String toReturn = "";
      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
      String line = null;
      try {
         while ((line = reader.readLine()) != null) {
            wr.append(line);
            OseeLog.log(StartJini.class, Level.INFO, "err: " + line + "\n");
            Matcher reggieStartMatcher = reggieStartPattern.matcher(wr);
            if (!wr.toString().contains(reggieFail)) {
               if (reggieStartMatcher.matches()) {
                  toReturn = reggieStartMatcher.group(1);
                  break;
               }
            } else {
               break;
            }
            wr.delete(0, wr.length());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
      return toReturn;
   }

   private String getHostName() {
      String host = "";
      try {
         host = InetAddress.getLocalHost().getCanonicalHostName();
      } catch (UnknownHostException ex) {
         host = "Error Obtaining Host";
         ex.printStackTrace();
      }
      return host;
   }

   private static InputStream getManifestFile(String[] args) {
      InputStream toReturn = null;
      CmdLineArgs cmdLineArgs = new CmdLineArgs(args);
      String manifestFileString = cmdLineArgs.get("-manifest");

      if (manifestFileString == null || manifestFileString.length() == 0) {
         manifestFileString = "META-INF/MANIFEST.MF";
      }
      File file = new File(manifestFileString);

      if (file == null || !file.exists()) {
         OseeLog.log(StartJini.class, Level.SEVERE, "The Specified Manifest File does not exist!!");
         System.exit(1);
      }
      try {
         toReturn = new FileInputStream(file);
      } catch (FileNotFoundException ex) {
         OseeLog.log(StartJini.class, Level.SEVERE, "The Specified Manifest File can not be opened!!", ex);
         System.exit(1);
      }
      return toReturn;
   }

   private static Dictionary<Object, Object> getHeaders(InputStream manifestFile) {
      try {
         return NonEclipseManifestHeader.parseManifest(manifestFile);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
      return null;
   }

   public static void main(String[] args) {
      OseeLog.log(StartJini.class, Level.INFO, "num args + " + args.length);
      InputStream manifestFile = getManifestFile(args);
      if (args.length == 1) {
         new StartJini(args[0], false, false, null, manifestFile);
      } else if (args.length > 1) {
         boolean browser = false, nohup = false;

         for (int i = 1; i < args.length; i++) {
            if (args[i].equalsIgnoreCase("-nohup")) {
               OseeLog.log(StartJini.class, Level.INFO, "nohup!!");
               nohup = true;
            } else if (args[i].equalsIgnoreCase("-browser")) {
               browser = true;
            }
         }

         new StartJini(args[0], nohup, browser, null, manifestFile);
      } else {
         OseeLog.log(StartJini.class, Level.INFO,
            "USAGE: -Dosee.jini.lookup.groups=<groups> StartJini <port> ?<-nohup> ?<-browser>");
      }

      OseeLog.log(StartJini.class, Level.INFO, "Exiting...");
      Runtime.getRuntime().exit(0);
   }

   private void killProcesses() {
      OseeLog.log(StartJini.class, Level.INFO, "Destroying Spawned Processes...");
      for (Process process : jiniProcesses) {
         if (process != null) {
            process.destroy();
         }
      }
   }

   @Override
   public void kill() throws RemoteException {
      OseeLog.log(StartJini.class, Level.INFO, "De-registering Core Jini Service...");
      deregisterService();
      killProcesses();
      ServiceDataStore.getNonEclipseInstance().terminate();
      commitSuicide();
   }
}