Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/tools/dash/StartFromConfig.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.tools.dash; | |
2 | |
3 import java.rmi.RMISecurityManager; | |
4 | |
5 import javax.swing.JOptionPane; | |
6 | |
7 import java.security.PrivilegedExceptionAction; | |
8 | |
9 import javax.security.auth.Subject; | |
10 | |
11 import javax.security.auth.login.LoginContext; | |
12 | |
13 import net.jini.config.Configuration; | |
14 import net.jini.config.ConfigurationProvider; | |
15 | |
16 import net.jini.lookup.ServiceDiscoveryManager; | |
17 | |
18 import net.jini.lookup.entry.Name; | |
19 | |
20 import net.jini.core.lookup.ServiceTemplate; | |
21 import net.jini.core.lookup.ServiceItem; | |
22 | |
23 import net.jini.core.entry.Entry; | |
24 | |
25 import net.jini.discovery.DiscoveryManagement; | |
26 | |
27 import net.jini.admin.Administrable; | |
28 | |
29 import net.jini.space.JavaSpace; | |
30 | |
31 import net.jini.security.ProxyPreparer; | |
32 import net.jini.security.BasicProxyPreparer; | |
33 | |
34 import org.dancres.blitz.remote.StatsAdmin; | |
35 | |
36 /** | |
37 Use this class to start dashboard from a JINI config file. | |
38 If you wish to use dashboard in a secure configuration, you'll need this! | |
39 | |
40 See page 43 of Murph's slides on client boilerplate | |
41 | |
42 Add preparer for lookup service | |
43 */ | |
44 public class StartFromConfig { | |
45 private static final String MODULE = "org.dancres.blitz.dash"; | |
46 private static final String LOGIN = "loginContext"; | |
47 private static final String SPACENAME = "spaceName"; | |
48 private static final String DM = "discoveryManagement"; | |
49 private static final String SPACE_PREP = "javaspacePreparer"; | |
50 private static final String ADMIN_PREP = "adminPreparer"; | |
51 private static final String TIMEOUT = "lookupTimeout"; | |
52 | |
53 public static void main(String args[]) { | |
54 try { | |
55 System.setSecurityManager(new RMISecurityManager()); | |
56 | |
57 final Configuration myConfig = | |
58 ConfigurationProvider.getInstance(args); | |
59 | |
60 final LoginContext myContext = | |
61 (LoginContext) | |
62 myConfig.getEntry(MODULE, LOGIN, LoginContext.class); | |
63 | |
64 if (myContext == null) { | |
65 StartFromConfig myStart = new StartFromConfig(myConfig); | |
66 myStart.startup(); | |
67 } else { | |
68 myContext.login(); | |
69 | |
70 Subject.doAsPrivileged(myContext.getSubject(), | |
71 new PrivilegedExceptionAction() { | |
72 public Object run() throws Exception { | |
73 StartFromConfig myStart = | |
74 new StartFromConfig(myConfig); | |
75 myStart.startup(); | |
76 | |
77 return null; | |
78 } | |
79 }, | |
80 null); | |
81 } | |
82 | |
83 } catch (Exception anE) { | |
84 System.err.println("Encountered error during startup, quitting"); | |
85 anE.printStackTrace(System.err); | |
86 System.exit(-1); | |
87 } | |
88 } | |
89 | |
90 private Configuration theConfig; | |
91 private ServiceDiscoveryManager theSDM; | |
92 private DiscoveryManagement theDM; | |
93 | |
94 private StartFromConfig(Configuration aConfig) { | |
95 theConfig = aConfig; | |
96 } | |
97 | |
98 private void startup() throws Exception { | |
99 try { | |
100 startupImpl(); | |
101 } finally { | |
102 if (theSDM != null) | |
103 try { | |
104 theSDM.terminate(); | |
105 } catch (Throwable aT) { | |
106 } | |
107 | |
108 if (theDM != null) | |
109 try { | |
110 theDM.terminate(); | |
111 } catch (Throwable aT) { | |
112 } | |
113 } | |
114 } | |
115 | |
116 private void startupImpl() throws Exception { | |
117 ProxyPreparer mySpacePrep = (ProxyPreparer) | |
118 theConfig.getEntry(MODULE, SPACE_PREP, ProxyPreparer.class, | |
119 new BasicProxyPreparer()); | |
120 | |
121 ProxyPreparer myAdminPrep = (ProxyPreparer) | |
122 theConfig.getEntry(MODULE, ADMIN_PREP, ProxyPreparer.class, | |
123 new BasicProxyPreparer()); | |
124 | |
125 theDM = (DiscoveryManagement) | |
126 theConfig.getEntry(MODULE, DM, DiscoveryManagement.class); | |
127 | |
128 theSDM = new ServiceDiscoveryManager(theDM, null, theConfig); | |
129 | |
130 Name myServiceName = new Name((String) | |
131 theConfig.getEntry(MODULE, | |
132 SPACENAME, | |
133 String.class)); | |
134 | |
135 ServiceTemplate myTemplate = | |
136 new ServiceTemplate(null, new Class[] {JavaSpace.class}, | |
137 new Entry[] {myServiceName}); | |
138 | |
139 long myTimeout = | |
140 ((Long) theConfig.getEntry(MODULE, TIMEOUT, Long.class, | |
141 new Long(30 * 1000))).longValue(); | |
142 | |
143 ServiceItem myResult = theSDM.lookup(myTemplate, null, myTimeout); | |
144 | |
145 if (myResult == null) { | |
146 JOptionPane.showMessageDialog(null, "Didn't find the specified JavaSpace :(", "Blitz Dash", JOptionPane.ERROR_MESSAGE); | |
147 return; | |
148 } | |
149 | |
150 JavaSpace mySpace = (JavaSpace) mySpacePrep.prepareProxy(myResult.service); | |
151 if (mySpace instanceof Administrable) { | |
152 Administrable myAdministrable = (Administrable) mySpace; | |
153 | |
154 Object myAdmin = | |
155 myAdminPrep.prepareProxy(myAdministrable.getAdmin()); | |
156 | |
157 if (myAdmin instanceof StatsAdmin) { | |
158 StatsAdmin myStats = (StatsAdmin) myAdmin; | |
159 | |
160 DashBoardFrame myFrame = | |
161 new DashBoardFrame(StartDashBoard.VER, myStats, true); | |
162 | |
163 myFrame.setVisible(true); | |
164 } else { | |
165 JOptionPane.showMessageDialog(null, "Space didn't have StatsAdmin :(", "Blitz Dash", JOptionPane.ERROR_MESSAGE); | |
166 return; | |
167 } | |
168 } else { | |
169 JOptionPane.showMessageDialog(null, "Space wasn't Administrable :(", "Blitz Dash", JOptionPane.ERROR_MESSAGE); | |
170 return; | |
171 } | |
172 } | |
173 } |