1 | package org.dcache.cells; |
2 | |
3 | import java.io.PrintWriter; |
4 | import java.util.Map; |
5 | |
6 | import dmg.util.Args; |
7 | import dmg.cells.nucleus.CellEndpoint; |
8 | import dmg.cells.nucleus.CellInfo; |
9 | import dmg.cells.nucleus.CellMessage; |
10 | import dmg.cells.nucleus.NoRouteToCellException; |
11 | import dmg.cells.nucleus.CellMessageAnswerable; |
12 | import dmg.cells.nucleus.SerializationException; |
13 | |
14 | public class AbstractCellComponent |
15 | implements CellInfoProvider, |
16 | CellSetupProvider, |
17 | CellMessageSender, |
18 | CellLifeCycleAware |
19 | { |
20 | private CellEndpoint _endpoint; |
21 | |
22 | /** |
23 | * Implements CellInfoProvider interface. |
24 | */ |
25 | public void getInfo(PrintWriter pw) {} |
26 | |
27 | /** |
28 | * Implements CellInfoProvider interface. |
29 | */ |
30 | public CellInfo getCellInfo(CellInfo info) |
31 | { |
32 | return info; |
33 | } |
34 | |
35 | /** |
36 | * Implements CellSetupProvider interface. |
37 | */ |
38 | public void printSetup(PrintWriter pw) {} |
39 | |
40 | /** |
41 | * Implements CellSetupProvider interface. |
42 | */ |
43 | public void beforeSetup() {} |
44 | |
45 | /** |
46 | * Implements CellSetupProvider interface. |
47 | */ |
48 | public void afterSetup() {} |
49 | |
50 | /** |
51 | * Implements CellLifeCycleAware interface. |
52 | */ |
53 | public void afterStart() {} |
54 | |
55 | /** |
56 | * Implements CellLifeCycleAware interface. |
57 | */ |
58 | public void beforeStop() {} |
59 | |
60 | /** |
61 | * Implements CellMessageSender interface. |
62 | */ |
63 | public void setCellEndpoint(CellEndpoint endpoint) |
64 | { |
65 | _endpoint = endpoint; |
66 | } |
67 | |
68 | /** |
69 | * Implements CellMessageSender interface. |
70 | */ |
71 | protected CellEndpoint getCellEndpoint() |
72 | { |
73 | return _endpoint; |
74 | } |
75 | |
76 | /** |
77 | * Sends <code>envelope</code>. |
78 | * |
79 | * @param envelope the cell message to be sent. |
80 | * @throws SerializationException if the payload object of this |
81 | * message is not serializable. |
82 | * @throws NoRouteToCellException if the destination could not be |
83 | * reached. |
84 | */ |
85 | protected void sendMessage(CellMessage envelope) |
86 | throws SerializationException, |
87 | NoRouteToCellException |
88 | { |
89 | _endpoint.sendMessage(envelope); |
90 | } |
91 | |
92 | /** |
93 | * Sends <code>envelope</code>. The <code>callback</code> argument |
94 | * (which has to be non-null) allows to specify an object which is |
95 | * informed as soon as an has answer arrived or if the timeout has |
96 | * expired. |
97 | * |
98 | * @param envelope the cell message to be sent. |
99 | * @param callback specifies an object class which will be informed |
100 | * as soon as the message arrives. |
101 | * @param timeout is the timeout in msec. |
102 | * @exception SerializationException if the payload object of this |
103 | * message is not serializable. |
104 | */ |
105 | protected void sendMessage(CellMessage envelope, |
106 | CellMessageAnswerable callback, |
107 | long timeout) |
108 | throws SerializationException |
109 | { |
110 | _endpoint.sendMessage(envelope, callback, timeout); |
111 | } |
112 | |
113 | /** |
114 | * Sends <code>envelope</code> and waits <code>timeout</code> |
115 | * milliseconds for an answer to arrive. The answer will bypass |
116 | * the ordinary queuing mechanism and will be delivered before any |
117 | * other asynchronous message. The answer need to have the |
118 | * getLastUOID set to the UOID of the message send with |
119 | * sendAndWait. If the answer does not arrive withing the specified |
120 | * time interval, the method returns <code>null</code> and the |
121 | * answer will be handled as if it was an ordinary asynchronous |
122 | * message. |
123 | * |
124 | * @param envelope the cell message to be sent. |
125 | * @param timeout milliseconds to wait for an answer. |
126 | * @return the answer or null if the timeout was reached. |
127 | * @throws SerializationException if the payload object of this |
128 | * message is not serializable. |
129 | * @throws NoRouteToCellException if the destination |
130 | * couldnot be reached. |
131 | */ |
132 | protected CellMessage sendAndWait(CellMessage envelope, long timeout) |
133 | throws SerializationException, |
134 | NoRouteToCellException, |
135 | InterruptedException |
136 | { |
137 | return _endpoint.sendAndWait(envelope, timeout); |
138 | } |
139 | |
140 | /** |
141 | * Provides information about the host cell. |
142 | * |
143 | * Depending on the cell, a subclass of CellInfo with additional |
144 | * information may be returned instead. |
145 | * |
146 | * @return The cell information encapsulated in a CellInfo object. |
147 | */ |
148 | protected CellInfo getCellInfo() |
149 | { |
150 | return _endpoint.getCellInfo(); |
151 | } |
152 | |
153 | /** |
154 | * Returns the name of the cell hosting this component. |
155 | */ |
156 | protected String getCellName() |
157 | { |
158 | return getCellInfo().getCellName(); |
159 | } |
160 | |
161 | /** |
162 | * Returns the name of the domain hosting the cell hosting this |
163 | * component. |
164 | */ |
165 | protected String getCellDomainName() |
166 | { |
167 | return getCellInfo().getDomainName(); |
168 | } |
169 | |
170 | /** |
171 | * Returns the domain context. The domain context is shared by all |
172 | * cells in a domain. |
173 | */ |
174 | protected Map<String,Object> getDomainContext() |
175 | { |
176 | return _endpoint.getDomainContext(); |
177 | } |
178 | |
179 | /** |
180 | * Returns the cell command line arguments provided when the cell |
181 | * was created. |
182 | */ |
183 | protected Args getArgs() |
184 | { |
185 | return _endpoint.getArgs(); |
186 | } |
187 | } |