001: /**
002: * $Id: PSResource.java,v 1.12 2007/01/26 03:47:11 portalbld Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.server.mbeans;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Set;
020: import java.util.ArrayList;
021:
022: import javax.management.MBeanServerConnection;
023: import javax.management.ObjectName;
024: import javax.management.remote.JMXConnector;
025:
026: import com.sun.portal.admin.server.AdminServerUtil;
027: import com.sun.portal.admin.common.PSMBeanException;
028: import com.sun.portal.admin.common.context.PortalDomainContext;
029: import com.sun.portal.admin.common.context.PSConfigContext;
030: import com.sun.portal.util.Platform;
031:
032: /**
033: * This class represents a manageable/configurable Portal Server
034: * resource that can be instrumented as a Portal Server MBean
035: * maintained by the Portal Admin Server.
036: * <p>
037: * Portal Server resources are organized in a hierarchical fashion.
038: * At the top is the Portal Domain object. Each portal domain
039: * consists of one or more portal (site) objects and other portal
040: * server resources that are independent of portals. Each portal
041: * consists of one or more portal server instance objects and other
042: * portal server resources that are independent of portal server
043: * instances but are associated with a portal.
044: * <p>
045: * The information about the presence of a particular instance of a
046: * portal server resource is stored in the portal domain repository.
047: * Each such instance is uniquely identified in the portal domain
048: * repository by a path, similar to the full path of a file in a file
049: * system or the DN of a LDAP entry in a LDAP directory. For example,
050: * the path to identify a particular portal server instance object is
051: * {instanceID, portalID, domainID}, where the first element of the
052: * path is the ID of that resource instance within its parent resource
053: * instance, and the last element is (almost) always the ID of the
054: * portal domain where this resource instance belongs to.
055: * <p>
056: * Each portal server resource instance is represented by a MBean and
057: * its life cycle is managed by the Portal Admin Server. When a
058: * Portal Admin Server instance is started, it looks into the portal
059: * domain repository and registers a MBean for each portal server
060: * resource instance in that portal domain.
061: */
062: public abstract class PSResource implements PSMBean {
063: protected PSConfigContext cc = null;
064: protected PortalDomainContext pdc = null;
065: protected List path = null;
066: protected String m_sPsID = null;
067: protected String m_sPsDomainID = null;
068: protected final static String fs = Platform.fs;
069:
070: /**
071: * Initializes this portal server resource with the given Portal
072: * Server configuration context, portal domain context, and path.
073: *
074: * @param cc the Portal Server configuration context.
075: * @param pdc the portal domain context.
076: * @param path the path to identify this portal resource instance.
077: * @exception NullPointerException if cc, pdc, or path is <code>null</code>.
078: * @exception IllegalArgumentException if path is empty.
079: */
080: public void init(PSConfigContext cc, PortalDomainContext pdc,
081: List path) {
082: if (cc == null) {
083: throw new NullPointerException("cc is null.");
084: }
085:
086: if (pdc == null) {
087: throw new NullPointerException("pdc is null.");
088: }
089:
090: if (path == null) {
091: throw new NullPointerException("path is null.");
092: }
093:
094: if (path.isEmpty()) {
095: throw new IllegalArgumentException("path cannot be empty.");
096: }
097:
098: this .cc = cc;
099: this .pdc = pdc;
100: this .path = path;
101: this .m_sPsDomainID = pdc.getID();
102: this .m_sPsID = (String) path.get(0);
103: }
104:
105: /**
106: * Returns the Portal Server configuration context.
107: *
108: * @return the Portal Server configuration context.
109: */
110: public PSConfigContext getPSConfigContext() {
111: return cc;
112: }
113:
114: /**
115: * Returns the context of the portal domain this portal server
116: * resource instance belongs to.
117: *
118: * @return the portal domain context.
119: */
120: public PortalDomainContext getPortalDomainContext() {
121: return pdc;
122: }
123:
124: /**
125: * Returns the ID of the portal domain this portal server resource
126: * instance belongs to.
127: *
128: * @return the portal domain ID.
129: */
130: public String getDomainID() {
131: return m_sPsDomainID;
132: }
133:
134: /**
135: * Returns the path to identify this portal server resource
136: * instance in the portal domain context.
137: *
138: * @return the path of this portal server resource instance.
139: */
140: public List getPath() {
141: return path;
142: }
143:
144: /**
145: * Returns the ID of this portal server resource instance.
146: *
147: * @return the ID of this portal server resource instance.
148: */
149: public String getID() {
150: return m_sPsID;
151: }
152:
153: /**
154: * Returns the sets of values of the resource attributes with the
155: * given names. The returned value is a map whose keySet is the
156: * set of attribute names given and each value of the map is a set
157: * of values of the corresponding attribute name as key. If any
158: * attribute has no value, the corresponding map value is an empty
159: * Set.
160: *
161: * @param type the type of the Portal Server resource.
162: * @param names the set of attribute names.
163: * @return a Map whose keySet is the Set of attribute names given
164: * and whose values are the Sets of attribute values.
165: * @exception PSMBeanException if an error occurs when getting the
166: * sets of attribute values.
167: */
168: public Map getMultipleAttributeValues(String type, Set names)
169: throws PSMBeanException {
170:
171: try {
172: return pdc.getMultipleAttributeValues(type, path, names);
173: } catch (Exception e) {
174: throw new PSMBeanException(
175: "admin.error.getMultipleAttributeValues", e
176: .getMessage(), e);
177: }
178: }
179:
180: /**
181: * Sets the resource attributes with the given names to the given
182: * values, replacing the old values if there were any. The keys
183: * of the given attributes map are the attribute names and the
184: * values are Sets of String values.
185: *
186: * @param type the type of the Portal Server resource.
187: * @param attributes a map of name-values pairs where each key
188: * is an attribute name and the corresponding
189: * value is a Set of String values.
190: * @exception PSMBeanException if an error occurs when setting the
191: * attribute values.
192: */
193: public void setMultipleAttributeValues(String type, Map attributes)
194: throws PSMBeanException {
195:
196: try {
197: pdc.setMultipleAttributeValues(type, path, attributes);
198: } catch (Exception e) {
199: throw new PSMBeanException(
200: "admin.error.setMultipleAttributeValues", e
201: .getMessage(), e);
202: }
203: }
204:
205: /**
206: * Returns the set of values of the resource attribute with the
207: * given name. If the attribute has no value, an empty Set is returned.
208: *
209: * @param type the type of the Portal Server resource.
210: * @param name name of the attribute.
211: * @return a Set of Strings.
212: * @exception PSMBeanException if an error occurs when getting the
213: * attribute values.
214: */
215: public Set getAttributeValues(String type, String name)
216: throws PSMBeanException {
217:
218: try {
219: return pdc.getAttributeValues(type, path, name);
220: } catch (Exception e) {
221: throw new PSMBeanException(
222: "admin.error.getAttributeValues", e.getMessage(),
223: e, new String[] { name });
224: }
225: }
226:
227: /**
228: * Sets the values of the resource attribute with the given name
229: * to the given values, replacing the old values if there were any.
230: *
231: * @param type the type of the Portal Server resource.
232: * @param name name of the attribute.
233: * @param values values of the attribute to be set to.
234: * @exception PSMBeanException if an error occurs when setting the
235: * attribute values.
236: */
237: public void setAttributeValues(String type, String name, Set values)
238: throws PSMBeanException {
239:
240: try {
241: pdc.setAttributeValues(type, path, name, values);
242: } catch (Exception e) {
243: throw new PSMBeanException(
244: "admin.error.setAttributeValues", e.getMessage(),
245: e, new String[] { name });
246: }
247: }
248:
249: /**
250: * Returns the value of the resource attribute with the given
251: * name. If the attribute has more than one values, the first one
252: * is returned. If the attribute has no value, <code>null</code>
253: * is returned.
254: *
255: * @param type the type of the Portal Server resource.
256: * @param name name of the attribute.
257: * @return the first value if more than one; <code>null</code> if no value.
258: * @exception PSMBeanException if an error occurs when getting the
259: * attribute value.
260: */
261: public String getAttributeValue(String type, String name)
262: throws PSMBeanException {
263:
264: try {
265: return pdc.getAttributeValue(type, path, name);
266: } catch (Exception e) {
267: throw new PSMBeanException("admin.error.getAttributeValue",
268: e.getMessage(), e, new String[] { name });
269: }
270: }
271:
272: /**
273: * Sets the value of the resource attribute with the given name to
274: * the given value, replacing the old values if there were any.
275: *
276: * @param type the type of the Portal Server resource.
277: * @param name name of the attribute.
278: * @param value value of the attribute to be set to.
279: * @exception PSMBeanException if an error occurs when setting the
280: * attribute value.
281: */
282: public void setAttributeValue(String type, String name, String value)
283: throws PSMBeanException {
284:
285: try {
286: pdc.setAttributeValue(type, path, name, value);
287: } catch (Exception e) {
288: throw new PSMBeanException("admin.error.setAttributeValue",
289: e.getMessage(), e, new String[] { name });
290: }
291: }
292:
293: /**
294: * Invokes a method on a remote host be connecting to the Cacao MBean
295: * server on the host sepecified in the params.
296: *
297: * @param host FQDN hostname of the target host
298: * @param on MBean Object Name
299: * @param operationName The method name to be invoked
300: * @param params method params as an Object array
301: * @param signature method signature definined in a String array
302: * @throws com.sun.portal.admin.common.PSMBeanException exceptions
303: * @return an Object resulting in the method invocation
304: */
305: public Object invokeRemoteOperation(String host,
306: ObjectName mBeanName, String operationName,
307: Object[] params, String[] signature)
308: throws PSMBeanException {
309:
310: JMXConnector connector = null;
311: try {
312: connector = AdminServerUtil.getJMXConnector(host);
313: MBeanServerConnection s = connector
314: .getMBeanServerConnection();
315: return s
316: .invoke(mBeanName, operationName, params, signature);
317: } catch (Exception e) {
318: throw new PSMBeanException(
319: "admin.error.invokeRemoteOperation",
320: e.getMessage(), e, new Object[] { operationName,
321: host });
322: } finally {
323: if (connector != null) {
324: try {
325: connector.close();
326: } catch (Exception e) {
327: // Ignore
328: }
329: }
330: }
331: }
332:
333: protected boolean isResourceAvailable(String type, String id)
334: throws PSMBeanException {
335:
336: try {
337: List resources = new ArrayList(pdc.getResourceIDs(type,
338: path));
339: if (resources.contains(id)) {
340: return true;
341: } else {
342: return false;
343: }
344:
345: } catch (Exception e) {
346: String message = "Error reading resources in domain "
347: + pdc.getID();
348: throw new PSMBeanException("admin.error.get.resources", e
349: .getMessage(), e, new Object[] { pdc.getID() });
350: }
351: }
352:
353: /**
354: * Returns a string representation of the stack trace.
355: * Puts the exception message in the first line and the rest of the
356: * stack trace below it, each terminated by a newline char.
357: */
358: public String getStackTrace(Throwable t) {
359: StringBuffer strace = new StringBuffer();
360: StackTraceElement[] ste = t.getStackTrace();
361: strace.append(t.getMessage());
362: strace.append('\n');
363: for (int i = 0; i < ste.length; i++) {
364: strace.append(ste[i].toString());
365: strace.append('\n');
366: }
367: return strace.toString();
368: }
369:
370: }
|