KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jmx > server > ServerContext


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.jmx.server;
5
6 import org.oddjob.arooa.registry.ComponentRegistry;
7 import org.oddjob.arooa.registry.ServerId;
8 import org.oddjob.jmx.RemoteOddjob;
9 import org.oddjob.logging.ConsoleArchiver;
10 import org.oddjob.logging.LogArchiver;
11 import org.oddjob.util.ThreadManager;
12
13 /**
14  * Provide a server context which can be passed down through the nodes
15  * of the server and used to look up useful things.
16  *
17  * @author Rob Gordon.
18  */

19 public class ServerContext {
20
21     // there can only ever be one of these.
22
private final ServerModel model;
23     
24     // these change down the hierarchy.
25

26     private final Object JavaDoc node;
27     
28     private final ComponentRegistry serverRegistry;
29     private final ComponentRegistry originalRegistry;
30     
31     private final LogArchiver logArchiver;
32     private final ConsoleArchiver consoleArchiver;
33
34     /** Indicate that we have looped back on ourselves */
35     private final boolean duplicate;
36     
37     private final String JavaDoc id;
38     private boolean registryOwner;
39     private String JavaDoc childId;
40     
41     /**
42      * A constructor for the top most server
43      * context.
44      */

45     public ServerContext(ServerModel model) {
46         this.model = model;
47         
48         this.logArchiver = model.getLogArchiver();
49         this.consoleArchiver = model.getConsoleArchiver();
50
51         this.node = model.getRoot();
52         
53         // The topmost node is never duplicated.
54
this.duplicate = false;
55
56         this.serverRegistry = new ComponentRegistry(new ServerId(model.getUrl()));
57         this.originalRegistry = model.getComponentRegistry();
58         this.id = originalRegistry.getIdForComponent(node);
59         serverRegistry.register(id, node);
60     }
61     
62     /**
63      * Create a context with a parent.
64      *
65      * @param parent The parent.
66      */

67     public ServerContext(Object JavaDoc node, ServerContext parent) {
68         // sanity check - this should never happen.
69
if (parent.duplicate) {
70             throw new IllegalStateException JavaDoc("Children of a duplicate are not allowed!");
71         }
72         
73         this.model = parent.model;
74         this.node = node;
75         
76         if (parent.node instanceof LogArchiver) {
77             logArchiver = (LogArchiver) parent.node;
78         }
79         else {
80             logArchiver = parent.logArchiver;
81         }
82         if (parent.node instanceof ConsoleArchiver) {
83             consoleArchiver = (ConsoleArchiver) parent.node;
84         }
85         else {
86             consoleArchiver = parent.consoleArchiver;
87         }
88         
89         // if we're serving nodes that are already remote check we
90
// haven't looped back on ourselves.
91
if (node instanceof RemoteOddjob) {
92             String JavaDoc remoteUrl = ((RemoteOddjob) node).serverInfo().getUrl();
93             if (remoteUrl.equals(model.getUrl())) {
94                 this.duplicate = true;
95             }
96             else {
97                 this.duplicate = false;
98             }
99         }
100         else {
101             this.duplicate = false;
102         }
103         
104         // start of a new registry?
105
ComponentRegistry childRegistry = parent.originalRegistry
106                 .registryOwnedBy(parent.node);
107         if (childRegistry == null) {
108             parent.registryOwner = false;
109             parent.childId = null;
110             this.originalRegistry = parent.originalRegistry;
111             this.serverRegistry = parent.serverRegistry;
112         }
113         else {
114             this.originalRegistry = childRegistry;
115             // has just become a registry owner
116
if (!parent.registryOwner) {
117                 parent.registryOwner = true;
118                 parent.childId = childRegistry.getIdForComponent(parent.node);
119                 ServerId serverId = originalRegistry.getServerId();
120                 if (serverId.equals(ServerId.local())) {
121                     serverId = new ServerId(model.getUrl());
122                 }
123                 this.serverRegistry = new ComponentRegistry(serverId);
124                 parent.serverRegistry.addChild(serverRegistry, parent.node);
125                 serverRegistry.register(parent.childId, parent.node);
126             }
127             else {
128                 this.serverRegistry = parent.serverRegistry.registryOwnedBy(parent.node);
129             }
130         }
131         
132         this.id = originalRegistry.getIdForComponent(node);
133         serverRegistry.register(id, node);
134     }
135     
136     public void removeChild(Object JavaDoc child) {
137         serverRegistry.remove(child);
138         if (!registryOwner) {
139             return;
140         }
141         // check the registry is still an owner - this is for oddjob which
142
// isn't after it destroys it's child.
143
ComponentRegistry originalChild = originalRegistry.registryOwnedBy(node);
144         if (originalChild == null) {
145             serverRegistry.removeChild(node);
146         }
147         registryOwner = false;
148         childId = null;
149     }
150     
151     /**
152      * @return Returns the uniqueId.
153      */

154     public String JavaDoc getUrl() {
155         return serverRegistry.getServerId().toString();
156     }
157             
158     /**
159      * @return Returns the threadManager.
160      */

161     public ThreadManager getThreadManager() {
162         return model.getThreadManager();
163     }
164     
165     /**
166      *
167      * @return Returns the interfaceManagerFactory.
168      */

169     public InterfaceManagerFactory getInterfaceManagerFactory() {
170         return model.getInterfaceManagerFactory();
171     }
172     
173     public LogArchiver getLogArchiver() {
174         return logArchiver;
175     }
176         
177     public ConsoleArchiver getConsoleArchiver() {
178         return consoleArchiver;
179     }
180
181     public ComponentRegistry getComponentRegistry() {
182         return serverRegistry;
183     }
184     
185     public String JavaDoc getChildId() {
186         return childId;
187     }
188
189     public String JavaDoc getId() {
190         return id;
191     }
192
193     public boolean isRegistryOwner() {
194         return registryOwner;
195     }
196
197     public boolean isDuplicate() {
198         return duplicate;
199     }
200 }
201
Popular Tags