001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.system.main;
017:
018: import java.io.PrintStream;
019: import java.net.InetSocketAddress;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.geronimo.gbean.AbstractName;
031: import org.apache.geronimo.gbean.AbstractNameQuery;
032: import org.apache.geronimo.gbean.GAttributeInfo;
033: import org.apache.geronimo.gbean.GBeanData;
034: import org.apache.geronimo.gbean.GBeanInfo;
035: import org.apache.geronimo.kernel.Kernel;
036: import org.apache.geronimo.kernel.management.State;
037:
038: /**
039: * @version $Rev: 629529 $ $Date: 2008-02-20 08:47:01 -0800 (Wed, 20 Feb 2008) $
040: */
041: public class StartupMonitorUtil {
042: private final static Log log = LogFactory
043: .getLog(StartupMonitor.class.getName());
044:
045: public static synchronized void wrapUp(PrintStream out,
046: Kernel kernel) {
047: List apps = new ArrayList(); // type = String (message)
048: List webs = new ArrayList(); // type = WebAppInfo
049: List ports = new ArrayList(); // type = AddressHolder
050: Map failed = new HashMap(); // key = AbstractName, value = String (message)
051: String serverInfo = null;
052: try {
053: Set gbeans = kernel.listGBeans((AbstractNameQuery) null);
054: Map beanInfos = new HashMap(); // key = GBeanInfo, value = List (of attribute names)
055: for (Iterator it = gbeans.iterator(); it.hasNext();) {
056: AbstractName name = (AbstractName) it.next();
057: if (isApplicationModule(name)) {
058: apps.add(" "
059: + decodeModule(name
060: .getNameProperty("j2eeType"))
061: + ": " + name.getNameProperty("name"));
062: }
063: if (isWebModule(name)) {
064: webs.add(kernel.getAttribute(name, "contextPath")
065: .toString());
066: }
067:
068: int stateValue = kernel.getGBeanState(name);
069: if (stateValue != State.RUNNING_INDEX) {
070: GBeanData data = kernel.getGBeanData(name);
071: State state = State.fromInt(stateValue);
072: StringBuffer buf = new StringBuffer();
073: buf.append("(").append(state.getName());
074: // Since it's not unusual for a failure to be caused by a port binding failure
075: // we'll see if there's a likely looking port attribute in the config data
076: // for the GBean. It's a long shot, but hey.
077: if (data != null && data.getAttributes() != null) {
078: Map map = data.getAttributes();
079: for (Iterator it2 = map.keySet().iterator(); it2
080: .hasNext();) {
081: String att = (String) it2.next();
082: if (att.equals("port")
083: || att.indexOf("Port") > -1) {
084: buf.append(",").append(att).append("=")
085: .append(map.get(att));
086: }
087: }
088: }
089: buf.append(")");
090: failed.put(name, buf.toString());
091: continue;
092: }
093:
094: // Check if this is ServerInfo
095: GBeanInfo info = kernel.getGBeanInfo(name);
096: if (info
097: .getClassName()
098: .equals(
099: "org.apache.geronimo.system.serverinfo.ServerInfo")) {
100: serverInfo = (String) kernel.getAttribute(name,
101: "version");
102: }
103:
104: // Look for any SocketAddress properties
105: List list = (List) beanInfos.get(info);
106: if (list == null) {
107: list = new ArrayList(3);
108: beanInfos.put(info, list);
109: Set atts = info.getAttributes();
110: for (Iterator it2 = atts.iterator(); it2.hasNext();) {
111: GAttributeInfo att = (GAttributeInfo) it2
112: .next();
113: if (att.getType().equals(
114: "java.net.InetSocketAddress")) {
115: list.add(att);
116: }
117: }
118: }
119: for (int i = 0; i < list.size(); i++) {
120: GAttributeInfo att = (GAttributeInfo) list.get(i);
121: try {
122: InetSocketAddress addr = (InetSocketAddress) kernel
123: .getAttribute(name, att.getName());
124: if (addr == null) {
125: log.debug("No value for GBean " + name
126: + " attribute " + att.getName());
127: continue;
128: } else if (addr.getAddress() == null
129: || addr.getAddress().getHostAddress() == null) {
130: log.debug("Null address or host for GBean "
131: + name + " " + att.getName() + ": "
132: + addr.getAddress());
133: }
134: String attName = info.getName();
135: if (list.size() > 1) {
136: attName += " " + decamelize(att.getName());
137: } else if (info.getAttribute("name") != null) {
138: attName += " "
139: + kernel.getAttribute(name, "name");
140: }
141: ports.add(new AddressHolder(attName, addr));
142: } catch (IllegalStateException e) {
143: // We weren't able to load a port for this service -- that's a bummer
144: }
145: }
146: }
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150:
151: Collections.sort(apps);
152:
153: // Helpful output: list of ports we listen on
154: if (ports.size() > 0) {
155: Collections.sort(ports);
156: System.out.println(" Listening on Ports:");
157: int max = 0;
158: for (int i = 0; i < ports.size(); i++) {
159: AddressHolder holder = (AddressHolder) ports.get(i);
160: if (holder.getAddress().getAddress() != null
161: && holder.getAddress().getAddress()
162: .getHostAddress() != null) {
163: max = Math.max(max, holder.getAddress()
164: .getAddress().getHostAddress().length());
165: }
166: }
167: for (int i = 0; i < ports.size(); i++) {
168: AddressHolder holder = (AddressHolder) ports.get(i);
169: StringBuffer buf = new StringBuffer();
170: buf.append(" ");
171: if (holder.getAddress().getPort() < 10) {
172: buf.append(' ');
173: }
174: if (holder.getAddress().getPort() < 100) {
175: buf.append(' ');
176: }
177: if (holder.getAddress().getPort() < 1000) {
178: buf.append(' ');
179: }
180: if (holder.getAddress().getPort() < 10000) {
181: buf.append(' ');
182: }
183: buf.append(holder.getAddress().getPort()).append(' ');
184: String address = holder.getAddress().getAddress() == null
185: || holder.getAddress().getAddress()
186: .getHostAddress() == null ? "" : holder
187: .getAddress().getAddress().getHostAddress();
188: buf.append(address);
189: for (int j = address.length(); j <= max; j++) {
190: buf.append(' ');
191: }
192: buf.append(holder.getName());
193: out.println(buf.toString());
194: }
195: out.println();
196: }
197: // Helpful output: list of applications started
198: if (apps.size() > 0) {
199: out.println(" Started Application Modules:");
200: for (int i = 0; i < apps.size(); i++) {
201: out.println((String) apps.get(i));
202: }
203: out.println();
204: }
205: // Helpful output: Web URLs
206: if (webs.size() > 0) {
207: Collections.sort(webs);
208: out.println(" Web Applications:");
209: for (int i = 0; i < webs.size(); i++) {
210: out.println(" " + webs.get(i));
211: }
212: out.println();
213: }
214:
215: // Helpful output: list of GBeans that did not start
216: if (failed.size() > 0) {
217: out
218: .println(" WARNING: Some GBeans were not started successfully:");
219: for (Iterator it = failed.keySet().iterator(); it.hasNext();) {
220: AbstractName name = (AbstractName) it.next();
221: String state = (String) failed.get(name);
222: if (name.getNameProperty("name") != null) {
223: log.debug("Unable to start " + name + " " + state);
224: out.println(" " + name.getNameProperty("name")
225: + " " + state);
226: } else {
227: out.println(" " + name + " " + state);
228: }
229: }
230: out.println();
231: }
232:
233: StringBuffer msg = new StringBuffer();
234: msg.append("Geronimo Application Server started");
235: if (serverInfo != null) {
236: msg.append(" (version ").append(serverInfo).append(")");
237: }
238: out.println(msg.toString());
239: out.flush();
240: }
241:
242: private static boolean isApplicationModule(AbstractName abstractName) {
243: String type = abstractName.getNameProperty("j2eeType");
244: String app = abstractName.getNameProperty("J2EEApplication");
245: String name = abstractName.getNameProperty("name");
246: if (type != null && (app == null || app.equals("null"))) {
247: return (type.equals("WebModule")
248: || type.equals("J2EEApplication")
249: || type.equals("EJBModule")
250: || type.equals("AppClientModule") || type
251: .equals("ResourceAdapterModule"))
252: && !name.startsWith("geronimo/system");
253: }
254: return false;
255: }
256:
257: private static boolean isWebModule(AbstractName abstractName) {
258: String type = abstractName.getNameProperty("j2eeType");
259: return type != null && type.equals("WebModule");
260: }
261:
262: private static String decodeModule(String value) {
263: if (value.equals("WebModule")) {
264: return "WAR";
265: } else if (value.equals("J2EEApplication")) {
266: return "EAR";
267: } else if (value.equals("EJBModule")) {
268: return "JAR";
269: } else if (value.equals("AppClientModule")) {
270: return "CAR";
271: } else if (value.equals("ResourceAdapterModule")) {
272: return "RAR";
273: } else {
274: return "UNK";
275: }
276: }
277:
278: private static String decamelize(String s) {
279: if (s == null || s.equals("")) {
280: return s;
281: }
282: StringBuffer buf = new StringBuffer();
283: buf.append(Character.toUpperCase(s.charAt(0)));
284: for (int i = 1; i < s.length(); i++) {
285: if (Character.isUpperCase(s.charAt(i))) {
286: if (s.length() > i + 1
287: && Character.isLowerCase(s.charAt(i + 1))) {
288: buf.append(" ");
289: }
290: }
291: buf.append(s.charAt(i));
292: }
293: return buf.toString();
294: }
295:
296: private static class AddressHolder implements Comparable {
297: private String name;
298: private InetSocketAddress address;
299:
300: public AddressHolder(String name, InetSocketAddress address) {
301: this .name = name;
302: this .address = address;
303: }
304:
305: public String getName() {
306: return name;
307: }
308:
309: public void setName(String name) {
310: this .name = name;
311: }
312:
313: public InetSocketAddress getAddress() {
314: return address;
315: }
316:
317: public void setAddress(InetSocketAddress address) {
318: this .address = address;
319: }
320:
321: public int compareTo(Object o) {
322: AddressHolder other = (AddressHolder) o;
323: int value = address.getPort() - other.address.getPort();
324: return value == 0 ? address.getAddress().toString()
325: .compareTo(other.address.getAddress().toString())
326: : value;
327: }
328:
329: public String toString() {
330: StringBuffer buf = new StringBuffer(this .getClass()
331: .getSimpleName()
332: + ":");
333: buf.append(" name=").append(name);
334: buf.append(", address=").append(address.toString());
335: return buf.toString();
336: }
337:
338: }
339: }
|