01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2006 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id$
23: */
24: package com.bostechcorp.cbesb.console.server;
25:
26: import javax.servlet.http.HttpSession;
27:
28: import com.bostechcorp.cbesb.common.i18n.CoreMessageConstants;
29: import com.bostechcorp.cbesb.common.i18n.Messages;
30: import com.bostechcorp.cbesb.console.common.Constants;
31: import com.bostechcorp.cbesb.console.common.JmxConnectInfo;
32: import com.bostechcorp.cbesb.console.common.ServerSideException;
33: import com.bostechcorp.cbesb.console.jmxclient.JmxClient;
34: import com.bostechcorp.cbesb.console.jmxclient.JmxClientFactory;
35:
36: public abstract class JmxServiceServlet extends
37: ConsoleRemoteServiceServlet {
38:
39: private static final long serialVersionUID = -2040003366584021133L;
40:
41: public JmxConnectInfo jmxConnect() {
42: JmxConnectInfo result = null;
43: try {
44: result = new JmxConnectInfo();
45: JmxClient jmxClient = JmxClientFactory.getDefaultClient();
46: HttpSession session = this .getThreadLocalRequest()
47: .getSession();
48: session.setAttribute(Constants.JMX_CLIENT, jmxClient);
49: // jmxClient.getConnectInfo(result);
50: } catch (Exception e) {
51: result.setException(e);
52: } catch (Throwable t) {
53: result.setException((Exception) t);
54: }
55: return result;
56: }
57:
58: protected JmxClient getClientFromSession()
59: throws ServerSideException {
60: // get the cached JMX client
61: HttpSession session = this .getThreadLocalRequest().getSession();
62: if (session == null)
63: throw new ServerSideException((Messages
64: .get(CoreMessageConstants.NO_SESSION_ERROR)));
65: JmxClient jmxClient = (JmxClient) session
66: .getAttribute(Constants.JMX_CLIENT);
67:
68: if (jmxClient == null) {
69: JmxConnectInfo connectInfo = jmxConnect();
70: if (connectInfo.isError()) {
71: throw new ServerSideException((Messages
72: .get(CoreMessageConstants.NO_JMX_SERVER_ERROR)));
73: }
74: jmxClient = (JmxClient) session
75: .getAttribute(Constants.JMX_CLIENT);
76:
77: if (jmxClient == null) {
78: throw new ServerSideException((Messages
79: .get(CoreMessageConstants.NO_JMX_SERVER_ERROR)));
80: } else {
81: jmxClient.setSessionId(session.getId()
82: + getCurrentUserId());
83: }
84: } else {
85: //It is important here, if the jmx don't connect, it will reconnect
86: jmxClient.getConnectInfo();
87: jmxClient
88: .setSessionId(session.getId() + getCurrentUserId());
89: // System.out.println("SessionId:"+jmxClient.getSessionId()+getCurrentUserId());
90: }
91: return jmxClient;
92: }
93: }
|