001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)BaseServiceImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029:
030: package com.sun.esb.management.base.services;
031:
032: import java.io.File;
033: import java.io.Serializable;
034:
035: import javax.management.MBeanServerConnection;
036:
037: import com.sun.esb.management.common.ManagementRemoteException;
038: import com.sun.jbi.ui.common.FileTransferManager;
039: import com.sun.jbi.ui.common.JBIResultXmlBuilder;
040:
041: /**
042: * Common methods for uploading and downloading files
043: *
044: * @author graj
045: */
046: public abstract class BaseServiceImpl extends AbstractServiceImpl
047: implements Serializable {
048:
049: private static final long serialVersionUID = -1L;
050:
051: /** last uploaded object reference */
052: protected Object mLastUploadId = null;
053:
054: /** Constructor - Constructs a new instance of AbstractServiceImpl */
055: public BaseServiceImpl() {
056: super (null, false);
057: }
058:
059: /**
060: * Constructor - Constructs a new instance of AbstractServiceImpl
061: *
062: * @param serverConnection
063: */
064: public BaseServiceImpl(MBeanServerConnection serverConnection) {
065: super (serverConnection, false);
066: }
067:
068: /**
069: * Constructor - Constructs a new instance of AbstractServiceImpl
070: *
071: * @param serverConnection
072: * @param isRemoteConnection
073: */
074: public BaseServiceImpl(MBeanServerConnection serverConnection,
075: boolean isRemoteConnection) {
076: super (serverConnection, isRemoteConnection);
077: }
078:
079: /**
080: * this method uploads the local file to the server using
081: * FileTransferManager and returns the path to the file on the server's file
082: * system which can be used to do any operations on the file/file data on
083: * the server side. This is used to upload components ( se, bc), shared
084: * library or service assembly archive files to the server for remote
085: * install/deploy commands.
086: *
087: * @param localFilePath
088: * file path on the local file system.
089: * @return String for file path of the uploaded file on the server file
090: * system
091: * @throws ManagementRemoteException
092: * if error or exception occurs.
093: */
094: protected String uploadFile(String localFilePath)
095: throws ManagementRemoteException {
096: File uploadFile = new File(localFilePath);
097: String name = uploadFile.getName();
098:
099: try {
100: FileTransferManager ftpManager = new FileTransferManager(
101: this .getMBeanServerConnection());
102: String uploadedFilePath = ftpManager
103: .uploadArchive(uploadFile);
104: this .mLastUploadId = ftpManager.getLastUploadId();
105: return uploadedFilePath;
106: } catch (Exception ex) {
107: String jbiMgmtMsg = JBIResultXmlBuilder.createJbiResultXml(
108: getI18NBundle(),
109: "jbi.ui.client.remote.file.upload.error",
110: new Object[] { name }, ex);
111: throw new ManagementRemoteException(new Exception(
112: jbiMgmtMsg));
113: } finally {
114:
115: }
116: }
117:
118: /**
119: * this method tries to remove the last uploaded file on the server. It
120: * ingores all errrors if it can not remove file as the server will take
121: * care of removing the files during restarts. Since this call can be called
122: * any where we need to have local instance of ftmgr to talk to server.
123: */
124: protected void removeUploadedFile() {
125: if (this .mLastUploadId == null) {
126: return;
127: }
128:
129: try {
130: FileTransferManager ftpManager = new FileTransferManager(
131: this .getMBeanServerConnection());
132: ftpManager.removeUploadedArchive(this .mLastUploadId);
133: this .mLastUploadId = null;
134: } catch (Exception ex) {
135: // ignore any exceptions as removing the uploaded file is optional.
136: } finally {
137: this .mLastUploadId = null;
138: }
139: }
140:
141: /**
142: * this method downloads a file from the server
143: * @param dir dir to download the file into
144: * @param serverFilePath the file path in the server
145: * @returns String the name of the downloaded file
146: * @throws ManagementRemoteException
147: * if error or exception occurs.
148: */
149: public String downloadFile(String dir, String serverFilePath)
150: throws ManagementRemoteException {
151: try {
152: FileTransferManager ftpManager = new FileTransferManager(
153: this .getMBeanServerConnection());
154: String fileName = new File(serverFilePath).getName();
155: ftpManager.downloadArchive(serverFilePath, new File(dir,
156: fileName));
157: return fileName;
158: } catch (Exception ex) {
159: String jbiMgmtMsg = JBIResultXmlBuilder.createJbiResultXml(
160: getI18NBundle(),
161: "jbi.ui.client.remote.file.download.error",
162: new Object[] { serverFilePath }, ex);
163: throw new ManagementRemoteException(new Exception(
164: jbiMgmtMsg));
165: } finally {
166:
167: }
168: }
169: }
|