001: package hero.util;
002:
003: /**
004: *
005: * Bonita
006: * Copyright (C) 1999 Bull S.A.
007: * Bull 68 route de versailles 78434 Louveciennes Cedex France
008: * Further information: bonita@objectweb.org
009: *
010: * This library is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public
012: * License as published by the Free Software Foundation; either
013: * version 2.1 of the License, or any later version.
014: *
015: * This library is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public
021: * License along with this library; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023: * USA
024: *
025: *
026: --------------------------------------------------------------------------
027: * $Id$
028: *
029: --------------------------------------------------------------------------
030: */
031:
032: import java.net.MalformedURLException;
033: import java.net.URI;
034: import java.util.List;
035: import java.util.Properties;
036:
037: import javax.management.Attribute;
038: import javax.management.MBeanServer;
039: import javax.management.MBeanServerConnection;
040: import javax.management.MBeanServerFactory;
041: import javax.management.ObjectName;
042: import javax.management.remote.JMXConnector;
043: import javax.management.remote.JMXConnectorFactory;
044: import javax.management.remote.JMXServiceURL;
045: import javax.naming.Context;
046:
047: import org.objectweb.carol.util.configuration.ConfigurationRepository;
048: import org.objectweb.carol.util.configuration.ProtocolConfiguration;
049:
050: /**
051: * This class is currently JOnAS specific.
052: */
053: public class BonitaConfigClient {
054:
055: private ObjectName config;
056: private MBeanServerConnection server;
057: //private MBeanServer server;
058: private static BonitaConfigClient bc = null; // Singleton pattern
059:
060: public BonitaConfigClient() throws HeroException {
061: try {
062: ProtocolConfiguration protocolConfiguration = ConfigurationRepository
063: .getCurrentConfiguration();
064: String sCarolURL = protocolConfiguration.getProviderURL();
065: URI carolURL = new URI(sCarolURL);
066: String host = carolURL.getHost();
067: int portNb = carolURL.getPort();
068: String port = String.valueOf(portNb);
069: String url = null;
070: url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port
071: + "/jrmpconnector_jonas";
072: //tmp = tmp + "url="+url+" ";
073:
074: /*
075: CarolCurrentConfiguration carolConfig = CarolCurrentConfiguration.getCurrent();
076: Properties props = carolConfig.getRMIProperties("jrmp");
077: String sCarolURL = props.getProperty(Context.PROVIDER_URL);
078: URI carolURL = new URI(sCarolURL);
079: String host= carolURL.getHost();
080: int portNb = carolURL.getPort();
081: String port = String.valueOf(portNb);
082: String url = null;
083:
084: url = "service:jmx:rmi:///jndi/rmi://"+host+":" + port + "/jrmpconnector_jonas";*/
085: // Try to connect to the MBeanServer
086: JMXServiceURL connURL = null;
087: try {
088: connURL = new JMXServiceURL(url);
089: } catch (MalformedURLException e) {
090: throw new HeroException(
091: "Can't create JMXServiceURL with string: "
092: + url);
093: }
094: JMXConnector connector = null;
095: try {
096: connector = JMXConnectorFactory.newJMXConnector(
097: connURL, null);
098: } catch (MalformedURLException e1) {
099: throw new HeroException(
100: "there is no provider for the protocol in "
101: + url);
102: } catch (java.io.IOException e) {
103: throw new HeroException(
104: "Connector client cannot be made because of a communication problem (used URL: "
105: + url + ")");
106: }
107: try {
108: connector.connect(null);
109: server = connector.getMBeanServerConnection();
110: } catch (java.io.IOException ioe) {
111: throw new HeroException(
112: "connection could not be made because of a communication problem");
113: }
114:
115: config = new ObjectName(
116: "bonita:type=bonitaservice,name=Config");
117: } catch (Exception e) {
118: e.printStackTrace();
119: throw new HeroException(e.getMessage());
120: }
121: }
122:
123: public static BonitaConfigClient getInstance() throws HeroException {
124: if (bc == null) {
125: bc = new BonitaConfigClient();
126: }
127: return bc;
128: }
129:
130: public void setJms(boolean jms) throws HeroException {
131: try {
132: server.invoke(config, "updateJms",
133: new Object[] { new Boolean(jms) },
134: new String[] { "boolean" });
135: } catch (Exception e) {
136: throw new HeroException(e.getMessage());
137: }
138: }
139:
140: public void setHistoric(String value) throws HeroException {
141: try {
142: server.invoke(config, "updateHistoric",
143: new Object[] { value }, new String[] { ""
144: .getClass().getName() });
145: } catch (Exception e) {
146: throw new HeroException(e.getMessage());
147: }
148: }
149:
150: public void setLogLevel(String value) throws HeroException {
151: try {
152: server.invoke(config, "updateLogLevel",
153: new Object[] { value }, new String[] { ""
154: .getClass().getName() });
155: } catch (Exception e) {
156: throw new HeroException(e.getMessage());
157: }
158: }
159:
160: public void setTraceLevel(String value) throws HeroException {
161: try {
162: server.invoke(config, "updateTraceLevel",
163: new Object[] { value }, new String[] { ""
164: .getClass().getName() });
165: } catch (Exception e) {
166: throw new HeroException(e.getMessage());
167: }
168: }
169:
170: public boolean getJms() throws HeroException {
171: try {
172: return (((Boolean) server.getAttribute(config, "Jms"))
173: .booleanValue());
174: } catch (Exception e) {
175: throw new HeroException(e.getMessage());
176: }
177: }
178:
179: public String getHistoric() throws HeroException {
180: try {
181: return ((String) server.getAttribute(config, "Historic"));
182: } catch (Exception e) {
183: throw new HeroException(e.getMessage());
184: }
185: }
186:
187: public String getLogLevel() throws HeroException {
188: try {
189: return ((String) server.getAttribute(config, "LogLevel"));
190: } catch (Exception e) {
191: throw new HeroException(e.getMessage());
192: }
193: }
194:
195: public String getTraceLevel() throws HeroException {
196: try {
197: return ((String) server.getAttribute(config, "TraceLevel"));
198: } catch (Exception e) {
199: throw new HeroException(e.getMessage());
200: }
201: }
202: }
|