001: /**
002: * $Id: AdminClientUtil.java,v 1.6 2005/11/23 21:39:21 yue 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.common.util;
014:
015: import java.io.IOException;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileOutputStream;
019: import java.util.LinkedList;
020: import java.util.logging.Level;
021:
022: import javax.management.remote.JMXConnector;
023: import javax.management.MBeanServerConnection;
024: import javax.management.ObjectName;
025: import javax.management.MalformedObjectNameException;
026: import javax.management.MBeanException;
027:
028: import com.sun.cacao.agent.auth.CacaoCallbackHandler;
029: import com.sun.portal.admin.common.PSMBeanException;
030:
031: import java.util.logging.Level;
032: import java.util.logging.Logger;
033: import com.sun.portal.log.common.PortalLogger;
034:
035: /**
036: * This class provides misc. utility methods for Portal Admin Server clients.
037: */
038: public class AdminClientUtil extends AdminUtil {
039:
040: private static Logger logger = PortalLogger
041: .getLogger(AdminClientUtil.class);
042:
043: /**
044: * Returns the encoded SASL authenticationID containing the given
045: * user name and domainID.
046: *
047: * @param userName user name of the user.
048: * @param domainID ID of the portal domain the user belongs to.
049: * @return the encoded SASL authenticationID.
050: */
051:
052: public static String encodeAuthenticationId(String userName,
053: String domainID) {
054:
055: return JMX_DOMAIN + CacaoCallbackHandler.SEPARATOR + userName
056: + CacaoCallbackHandler.SEPARATOR + domainID;
057: }
058:
059: /**
060: * Returns the encoded SASL password containing the given password.
061: *
062: * @param password the password to log into the portal domain.
063: * @return the encoded SASL password.
064: */
065: public static String encodePassword(String password) {
066: return password;
067: }
068:
069: /**
070: * Returns a connected JMXConnector to the connector server
071: * running on the given host. The connection is authenticated
072: * against the portal domain with the given domain ID using the
073: * given user ID and password. The given user ID can either be
074: * the user DN, or the uid attribute value (in that case, the
075: * default org in the portal domain is assumed).
076: * <p>
077: * The caller is responsible for obtaining the
078: * MBeanServerConnection and closing the returned JMXConnector.
079: *
080: * @param host the host where the JMX connector server is running on.
081: * @param domainID the ID of the portal domain to authenticate against.
082: * @param userID the user ID (user DN or the uid attribute value).
083: * @param password the user password.
084: * @return a connected JMXConnector.
085: * @exception NullPointerException if host, domainID, userID or
086: * password is null.
087: * @exception SecurityException if an authentication error occurs.
088: * @exception IOException if a connection error occurs.
089: */
090: public static JMXConnector getJMXConnector(String host,
091: String domainID, String userID, String password)
092: throws SecurityException, IOException {
093:
094: if (host == null) {
095: throw new NullPointerException("host is null.");
096: }
097:
098: if (domainID == null) {
099: throw new NullPointerException("domainID is null.");
100: }
101:
102: if (userID == null) {
103: throw new NullPointerException("userID is null.");
104: }
105:
106: if (password == null) {
107: throw new NullPointerException("password is null.");
108: }
109:
110: String id = encodeAuthenticationId(userID, domainID);
111: String passwd = encodePassword(password);
112: return getConnector(host, id, passwd);
113: }
114:
115: /**
116: * Returns a connected JMXConnector to the connector server
117: * running on the local host. The connection is authenticated
118: * against the portal domain with the given domain ID using the
119: * given user ID and password. The given user ID can either be
120: * the user DN, or the uid attribute value (in that case, the
121: * default org in the portal domain is assumed).
122: * <p>
123: * The caller is responsible for obtaining the
124: * MBeanServerConnection and closing the returned JMXConnector.
125: *
126: * @param domainID the ID of the portal domain to authenticate against.
127: * @param userID the user ID (user DN or the uid attribute value).
128: * @param password the user password.
129: * @return a connected JMXConnector.
130: * @exception NullPointerException if domainID, userID or password is null.
131: * @exception SecurityException if an authentication error occurs.
132: * @exception IOException if a connection error occurs.
133: */
134: public static JMXConnector getJMXConnector(String domainID,
135: String userID, String password) throws SecurityException,
136: IOException {
137:
138: return getJMXConnector("localhost", domainID, userID, password);
139: }
140:
141: /**
142: * Returns a connected JMXConnector to the connector server
143: * running on the local host. The connection is authenticated
144: * against the default portal domain using the given user ID and
145: * password. The given user ID can either be the user DN, or the
146: * uid attribute value (in that case, the default org in the
147: * default portal domain is assumed).
148: * <p>
149: * The caller is responsible for obtaining the
150: * MBeanServerConnection and closing the returned JMXConnector.
151: *
152: * @param userID the user ID (user DN or the uid attribute value).
153: * @param password the user password.
154: * @return a connected JMXConnector.
155: * @exception NullPointerException if userID or password is null.
156: * @exception SecurityException if an authentication error occurs.
157: * @exception IOException if a connection error occurs.
158: */
159: public static JMXConnector getJMXConnector(String userID,
160: String password) throws SecurityException, IOException {
161:
162: return getJMXConnector(DEFAULT_DOMAIN, userID, password);
163: }
164:
165: /*
166: * This method makes connection to UploadDownloadFileManagerMBean and invokes upload command .
167: * @returns 2 elements array where first element is objectid and Second element is filename .
168: */
169: public static String[] uploadFile(MBeanServerConnection msc,
170: String domainId, File file, int uploadSize)
171: throws UploadDownloadException {
172: String remoteFileName = null;
173: ObjectName objectName = null;
174: String uploadId = null;
175: String[] nameIdArray = null;
176: long fileSize = file.length();
177: FileInputStream fi = null;
178:
179: if (uploadSize == 0) {
180: uploadSize = DEFAULT_UPLOAD_SIZE; //set the default uploadsize to 4K
181: }
182: try {
183:
184: LinkedList path = new LinkedList();
185: path.addFirst(domainId);
186: path.addFirst(FILE_UPLOAD_DOWNLOAD_MANAGER);
187: //get the UploadDownloadFileManagerMbean
188: objectName = AdminClientUtil.getResourceMBeanObjectName(
189: FILE_UPLOAD_DOWNLOAD_MANAGER_MBEAN_TYPE, path);
190: if (!(msc.isRegistered(objectName))) {
191: logger.log(Level.SEVERE, "PSPL_CSPACU0004");
192: throw new UploadDownloadException(
193: "mbean not registered");
194: }
195: //invoke initiateFileUpload on UploadDownloadFileManagerMbean
196: Object[] params = { file.getName(), new Long(fileSize) };
197: String[] signature = { "java.lang.String", "java.lang.Long" };
198: nameIdArray = (String[]) msc.invoke(objectName,
199: "initiateFileUpload", params, signature);
200: uploadId = nameIdArray[0];
201: remoteFileName = (String) nameIdArray[1];
202:
203: //invoke uploadBytes on UploadDownloadFileManagerMbean with a chunk size of "uploadSize"
204:
205: fi = new FileInputStream(file);
206: byte[] bytesToUpload = null; //array holding bytes read from local file
207: long offset = 0;
208: int readSize = 0; //no. of bytes read from local file
209:
210: String nameByteArray = (new byte[0]).getClass().getName();
211: signature[0] = "java.lang.String";
212: signature[1] = nameByteArray;
213: params[0] = uploadId;
214:
215: readSize = fileSize < uploadSize ? new Long(fileSize)
216: .intValue() : uploadSize;
217: bytesToUpload = new byte[readSize];
218:
219: while (fi.read(bytesToUpload) != -1) {
220: params[1] = bytesToUpload;
221: msc
222: .invoke(objectName, "uploadBytes", params,
223: signature);
224:
225: offset = offset + readSize;
226: readSize = new Long(fileSize - offset).intValue() > uploadSize ? uploadSize
227: : new Long(fileSize - offset).intValue();
228: if (readSize == 0) {
229: break;
230: }
231: bytesToUpload = new byte[readSize];
232: }
233:
234: } catch (MalformedObjectNameException moe) {
235: logger.log(Level.SEVERE, "PSPL_CSPACU0002", moe);
236: throw new UploadDownloadException(moe);
237:
238: } catch (IOException e) {
239: logger.log(Level.SEVERE, "PSPL_CSPACU0001", e);
240: throw new UploadDownloadException(e);
241: } catch (Exception e) {
242: logger.log(Level.SEVERE, "PSPL_CSPACU0003", e);
243: throw new UploadDownloadException(e);
244: } finally {
245: try {
246: fi.close();
247: } catch (IOException ioe) {
248: logger.log(Level.SEVERE, "PSPL_CSPACU0001", ioe);
249: }
250: }
251: return nameIdArray;
252: }
253:
254: /*
255: * This method makes connection to UploadDownloadFileManagerMBean and makes a clean up for upload/download
256: * @params id object id for the file uploaded or downloaded
257: */
258: public static void uploadDownloadCleanUp(MBeanServerConnection msc,
259: String domainId, String id) throws UploadDownloadException {
260: ObjectName objectName;
261: try {
262:
263: LinkedList path = new LinkedList();
264: path.addFirst(domainId);
265: path.addFirst(FILE_UPLOAD_DOWNLOAD_MANAGER);
266: //get the UploadDownloadFileManagerMbean
267: objectName = AdminClientUtil.getResourceMBeanObjectName(
268: "PortalDomain.UploadDownloadFileManager", path);
269: if (!(msc.isRegistered(objectName))) {
270: logger.log(Level.SEVERE, "PSPL_CSPACU0004");
271: throw new UploadDownloadException(
272: "mbean not registered");
273: }
274: String[] params = { id };
275: String[] signature = { "java.lang.String" };
276: msc.invoke(objectName, "cleanUp", params, signature);
277:
278: } catch (MalformedObjectNameException moe) {
279: logger.log(Level.SEVERE, "PSPL_CSPACU0002", moe);
280: throw new UploadDownloadException(moe);
281: } catch (IOException e) {
282: logger.log(Level.SEVERE, "PSPL_CSPACU0001", e);
283: } catch (Exception e) {
284: logger.log(Level.SEVERE, "PSPL_CSPACU0003", e);
285: throw new UploadDownloadException(e);
286: }
287:
288: }
289:
290: /*
291: * This method makes connection to UploadDownloadFileManagerMBean and downloads a file . The
292: * downloaded file gets created in current directory local filesystem.
293: * @params fileName should be absolute file name with the path
294: */
295: public static long downloadFile(MBeanServerConnection msc,
296: String domainId, String fileName, int downloadSize,
297: String destFileName) throws UploadDownloadException {
298:
299: ObjectName objectName = null;
300: String downloadId = null;
301: Object[] idSizeArray = null;
302: long fileSize = 0;
303: FileOutputStream fo = null;
304: if (downloadSize == 0) {
305: downloadSize = DEFAULT_DOWNLOAD_SIZE; //set the default downloadsize to 4K
306: }
307: try {
308: LinkedList path = new LinkedList();
309: path.addFirst(domainId);
310: path.addFirst(FILE_UPLOAD_DOWNLOAD_MANAGER);
311: //get the UploadDownloadFileManagerMbean
312: objectName = AdminClientUtil.getResourceMBeanObjectName(
313: "PortalDomain.UploadDownloadFileManager", path);
314: if (!(msc.isRegistered(objectName))) {
315: logger.log(Level.SEVERE, "PSPL_CSPACU0004");
316: throw new UploadDownloadException(
317: "mbean not registered");
318: }
319: //invoke initiateFileDownload on UploadDownloadFileManagerMbean
320: Object[] params = { fileName };
321: String[] signature = { "java.lang.String" };
322: idSizeArray = (Object[]) msc.invoke(objectName,
323: "initiateFileDownload", params, signature);
324: downloadId = (String) idSizeArray[0];
325: fileSize = ((Long) idSizeArray[1]).longValue();
326:
327: //invoke downloadBytes on UploadDownloadFileManagerMbean with a chunk size of "downloadSize"
328: byte[] bytesDownloaded = null;
329: int readSize;
330: long bytesRead = 0;
331:
332: String[] signature2 = { "java.lang.String",
333: "java.lang.Integer" };
334: Object[] params2 = new Object[2];
335: params2[0] = downloadId;
336:
337: fo = new FileOutputStream(destFileName, true); //create a file in current directory on local filesystem
338: readSize = fileSize > downloadSize ? downloadSize
339: : new Long(fileSize).intValue();
340:
341: while (bytesRead <= fileSize) {
342: params2[1] = new Integer(readSize);
343: bytesDownloaded = (byte[]) msc.invoke(objectName,
344: "downloadBytes", params2, signature2);
345: fo.write(bytesDownloaded);
346: bytesRead = bytesRead + bytesDownloaded.length;
347: readSize = (fileSize - bytesRead) > downloadSize ? downloadSize
348: : new Long(fileSize - bytesRead).intValue();
349: if (readSize == 0) {
350: break;
351: }
352: }
353: //close the inputstream on remote server
354: String[] params3 = { downloadId };
355: String[] signature3 = { "java.lang.String" };
356: msc.invoke(objectName, "cleanUp", params3, signature3);
357: } catch (MalformedObjectNameException moe) {
358: logger.log(Level.SEVERE, "PSPL_CSPACU0002", moe);
359: throw new UploadDownloadException(moe);
360: } catch (IOException e) {
361: logger.log(Level.SEVERE, "PSPL_CSPACU0001", e);
362: //throw new PSMBeanException(ERROR_FILE_DOWNLOAD,null);
363: throw new UploadDownloadException(e);
364: } catch (Exception e) {
365: logger.log(Level.SEVERE, "PSPL_CSPACU0003", e);
366: throw new UploadDownloadException(e);
367: } finally {
368: try {
369: fo.flush();
370: fo.close();
371: } catch (IOException ioe) {
372: logger.log(Level.SEVERE, "PSPL_CSPACU0001", ioe);
373: }
374: }
375: return fileSize;
376: }
377:
378: }
|