001: package com.sun.portal.sra.admin.mbeans;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.io.FileInputStream;
006: import java.lang.reflect.InvocationTargetException;
007: import java.lang.reflect.Method;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Properties;
012:
013: import javax.management.InstanceNotFoundException;
014: import javax.management.MBeanException;
015: import javax.management.MalformedObjectNameException;
016: import javax.management.ObjectName;
017: import javax.management.ReflectionException;
018:
019: import com.sun.portal.admin.common.context.PSConfigContext;
020: import com.sun.portal.admin.common.context.PortalDomainContext;
021: import com.sun.portal.admin.common.util.AdminUtil;
022: import com.sun.portal.admin.server.mbeans.PSResource;
023: import com.sun.portal.monitoring.*;
024: import com.sun.portal.monitoring.security.ssl.SslContext;
025: import com.sun.portal.monitoring.utilities.PropertyHelper;
026: import com.sun.portal.monitoring.utilities.UtilityException;
027:
028: import com.sun.portal.util.Platform;
029:
030: /**
031: * author: Noble Paul Date: Mar 17, 2005, 2:02:53 PM
032: */
033: public class SRAMonitoring extends PSResource implements
034: SRAMonitoringMBean {
035: protected Subsystem subsystem;
036:
037: protected Client client;
038:
039: protected String host;
040:
041: protected ObjectName objectNameProxy;
042:
043: private Map subsystemCache = new HashMap();
044:
045: private Map propertiesCache = new HashMap();
046:
047: public void init(PSConfigContext cc, PortalDomainContext pdc,
048: List path) {
049: super .init(cc, pdc, path);
050: try {
051: objectNameProxy = AdminUtil.getResourceMBeanObjectName(
052: "PortalDomain.SRAMonitoring", path);
053: } catch (MalformedObjectNameException mone) {
054: Debug.severe("PSMN_CSPM0001", new Object[] { Debug
055: .getStackTrace(mone) }, null);
056: }
057: }
058:
059: private Subsystem getSubSystem(String profile, String proxyType)
060: throws MonitoringException {
061: Subsystem subsystem = (Subsystem) subsystemCache.get(profile
062: + "." + proxyType);
063: if (subsystem != null) {
064: return subsystem;
065: }
066:
067: String configurationDirectory = cc.getPSConfigDir()
068: + Platform.fs + profile + Platform.fs + proxyType;
069: PropertyHelper ph = new PropertyHelper(new Properties());
070: try {
071: ph.loadProperties(configurationDirectory + Platform.fs
072: + "monitoring.properties");
073: } catch (UtilityException e) {
074: throw new MonitoringException(e);
075: }
076: ph.setProperty(ConnectorContext.class.getName(),
077: ConnectorContext.PROPERTY_SUFFIX_SSL_DISABLE, "true");
078: ph.setProperty(ConnectorContext.class.getName(),
079: ConnectorContext.PROPERTY_SUFFIX_SASL_DISABLE, "true");
080: ph
081: .setProperty(
082: MonitoringContext.class.getName(),
083: MonitoringContext.PROPERTY_SUFFIX_CONFIGURATION_DIRECTORY,
084: configurationDirectory);
085: ph.setProperty(MonitoringContext.class.getName(),
086: MonitoringContext.PROPERTY_SUFFIX_SUBSYSTEM_MODE,
087: "Client");
088: ph.setProperty(SslContext.class.getName(),
089: SslContext.PROPERTY_SUFFIX_TRUST_ALL_CERTIFICATES,
090: "true");
091:
092: subsystem = new SubsystemImpl(ph.getProperties());
093: subsystem.start();
094:
095: subsystemCache.put(profile + "." + proxyType, subsystem);
096: return subsystem;
097:
098: }
099:
100: public Object invoke(ObjectName on, String operationName,
101: Object[] params, String[] signature)
102: throws ReflectionException, InstanceNotFoundException,
103: IOException, MBeanException {
104: if (on == null && operationName == null) {
105: return invokeMethod(params);
106: } else {
107: return client.invoke(on, operationName, params, signature);
108: }
109: }
110:
111: private Object invokeMethod(Object[] params)
112: throws ReflectionException, MBeanException {
113: String methodName = (String) params[0];
114: String proxyType = (String) params[1];
115: String instanceName = (String) params[2];
116: Method method = null;
117: try {
118: method = getMethod(methodName, Client.class);
119: } catch (NoSuchMethodException e) {
120: throw new ReflectionException(e);
121: }
122: Object[] newParams = new Object[0];
123: if (params.length > 3) {
124: newParams = new Object[params.length - 3];
125: System.arraycopy(params, 3, newParams, 0, newParams.length);
126: }
127: Client c = null;
128: try {
129: c = getSubSystem(instanceName, proxyType).getClient();
130: } catch (Exception e) {
131: e.printStackTrace();// TODO handle Exception properly
132: throw new MBeanException(e);
133: }
134: try {
135: return method.invoke(c, newParams);
136: } catch (IllegalAccessException e) {
137: throw new ReflectionException(e);
138: } catch (InvocationTargetException e) {
139: throw new ReflectionException(e);
140: }
141: }
142:
143: private Method getMethod(String methodName, Class clzz)
144: throws NoSuchMethodException {
145: Method[] methods = clzz.getMethods();
146: for (int i = 0; i < methods.length; i++) {
147: if (methodName.equals(methods[i].getName())) {
148: return methods[i];
149: }
150: }
151: throw new NoSuchMethodException(methodName);
152: }
153:
154: public String getMonitoringProperty(String proxyType,
155: String instance, String propName) {
156: String result = (String) propertiesCache.get(proxyType + ":"
157: + instance + ":" + propName);
158: if (result != null) {
159: return result;
160: }
161: String monProperties = cc.getPSConfigDir() + Platform.fs
162: + instance + Platform.fs + proxyType + Platform.fs
163: + "monitoring.properties";
164: Properties properties = new Properties();
165: try {
166: properties.load(new FileInputStream(monProperties));
167: } catch (IOException e) {
168: return "";
169: }
170:
171: if ("true"
172: .equals(properties
173: .getProperty("com.sun.portal.monitoring.MonitoringContext.monitoring.disable"))) {
174: return "";
175: }
176: result = properties.getProperty(propName);
177: if (result != null) {
178: propertiesCache.put(proxyType + ":" + instance + ":"
179: + propName, result);
180: return result;
181: } else {
182: return "";
183: }
184: }
185:
186: public void disconnect(String proxyType, String instance)
187: throws IOException {
188: // try {
189: // Subsystem subsystem = getSubSystem(instance,proxyType);
190: // if(subsystem != null){
191: // try {
192: // subsystem.getClient().disconnect();
193: // } catch (Exception e) {}
194: // }
195: // } catch (MonitoringException e) {
196: // e.printStackTrace();//TODO handle Exception properly
197: // }
198:
199: }
200:
201: public void connect(String proxyType, String instance)
202: throws IOException {
203: try {
204: Subsystem subsystem = getSubSystem(instance, proxyType);
205: try {
206: subsystem.getClient();
207: } catch (MonitoringException me) {
208: client = null;
209: Debug.severe("PSMN_CSPM0001", new Object[] { Debug
210: .getStackTrace(me) }, null);
211: throw new IOException(Debug.getStackTrace(me));
212: }
213: } catch (MonitoringException me) {
214: Debug.severe("PSMN_CSPM0001", new Object[] { Debug
215: .getStackTrace(me) }, null);
216: throw new IOException(Debug.getStackTrace(me));
217: }
218: }
219:
220: }
|