1 | // jademx - JADE management using JMX |
2 | // Copyright 2005 Caboodle Networks, Inc. |
3 | // |
4 | // This library is free software; you can redistribute it and/or |
5 | // modify it under the terms of the GNU Lesser General Public |
6 | // License as published by the Free Software Foundation; either |
7 | // version 2.1 of the License, or (at your option) any later version. |
8 | // |
9 | // This library is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | // Lesser General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU Lesser General Public |
15 | // License along with this library; if not, write to the Free Software |
16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
17 | |
18 | |
19 | package jade.jademx.mbean; |
20 | |
21 | import jade.BootProfileImpl; |
22 | import jade.core.Profile; |
23 | import jade.core.Runtime; |
24 | import jade.util.Logger; |
25 | import jade.wrapper.PlatformController; |
26 | |
27 | /** |
28 | * class to be a thread for a JADE platform |
29 | * @author David Bernstein, <a href="http://www.caboodlenetworks.com" |
30 | * >Caboodle Networks, Inc.</a> |
31 | */ |
32 | public class JadeRunnable implements Runnable { |
33 | |
34 | /** JADE's runtime instance for this JVM */ |
35 | private static final Runtime runtimeInstance = Runtime.instance(); |
36 | /** JADE platform controller for this thread */ |
37 | private PlatformController platformController = null; |
38 | /** platform boot profile */ |
39 | private Profile profile; |
40 | /** my logger */ |
41 | private final Logger logger = |
42 | Logger.getMyLogger(JadeRunnable.class.getName()); |
43 | /** how long to sleep in ms for main loop */ |
44 | private final static long RUN_LOOP_SLEEP_MS = 60000; // 60,000 = 1 minute |
45 | /** is JADE platform controller active? */ |
46 | private boolean platformActive = false; |
47 | /** somebody listening for platform active state */ |
48 | private JadePlatformMonitor jadePlatformMonitor; |
49 | |
50 | /** |
51 | * make a Runnable for a JadePlatform |
52 | * @param profile boot parameters |
53 | * @param jadePlatformMonitor up/down listener |
54 | */ |
55 | public JadeRunnable( Profile profile, JadePlatformMonitor jadePlatformMonitor ) { |
56 | this.profile = profile; |
57 | this.jadePlatformMonitor = jadePlatformMonitor; |
58 | } |
59 | |
60 | /** |
61 | * set whether platform active and if set, notify monitor |
62 | * @param platformActive new platform active state |
63 | */ |
64 | private void setPlatformActive( boolean platformActive ) { |
65 | this.platformActive = platformActive; |
66 | if ( null != jadePlatformMonitor ) { |
67 | jadePlatformMonitor.setPlatformActive( platformActive ); |
68 | } |
69 | } |
70 | |
71 | /* (non-Javadoc) |
72 | * @see java.lang.Runnable#run() |
73 | */ |
74 | public void run() { |
75 | String threadDescription; |
76 | // if ( logger.isLoggable( Logger.FINER ) ) { |
77 | Thread currentThread = Thread.currentThread(); |
78 | ThreadGroup threadGroup = currentThread.getThreadGroup(); |
79 | String threadGroupName = threadGroup.getName(); |
80 | String threadName = currentThread.getName(); |
81 | String threadIdDescription = ""; |
82 | try { |
83 | long threadId = currentThread.getId(); |
84 | threadIdDescription = ",id=" + threadId; |
85 | } |
86 | catch ( Throwable t ) { |
87 | // Thread.getId() arrived at J2SE 5.0 so it's not supported |
88 | // in this environment just go on silently. |
89 | } |
90 | threadDescription = |
91 | "JADE platform thread "+ |
92 | "(group=\"" + threadGroupName + |
93 | "\",name=\"" + threadName + |
94 | threadIdDescription + ")"; |
95 | // } |
96 | // else { |
97 | // threadDescription = ""; |
98 | // } |
99 | |
100 | boolean isAgentContainer = |
101 | profile.getBooleanProperty( |
102 | BootProfileImpl.CONTAINER_KEY, false ); |
103 | logger.log( Logger.FINER, "isAgentContainer="+isAgentContainer); |
104 | boolean isMain = !isAgentContainer; |
105 | logger.log( Logger.FINER,"JadeRunnable.run(): isMain="+isMain); |
106 | logger.log( Logger.FINER, threadDescription+" starting..." ); |
107 | platformController = ( isMain |
108 | ? runtimeInstance.createMainContainer( profile ) |
109 | : runtimeInstance.createAgentContainer( profile ) ); |
110 | setPlatformActive( true ); |
111 | logger.log( Logger.FINER, threadDescription+" active" ); |
112 | while ( platformActive ) { |
113 | try { |
114 | Thread.sleep( RUN_LOOP_SLEEP_MS ); |
115 | } |
116 | catch ( InterruptedException ie ) { |
117 | setPlatformActive( false ); |
118 | } |
119 | } |
120 | logger.log( Logger.FINER, threadDescription+" shutting down..." ); |
121 | } |
122 | |
123 | |
124 | |
125 | /** |
126 | * get JADE platform controller for this thread. |
127 | * just for this package. |
128 | * @return Returns the platformController. |
129 | */ |
130 | PlatformController getPlatformController() { |
131 | return platformController; |
132 | } |
133 | } |