001: /**
002: * $Id: ImportExportBaseBean.java,v 1.14 2006/06/15 22:34:47 cathywu Exp $
003: * Copyright 2005 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.console.fabric;
014:
015: import java.beans.*;
016: import java.io.File;
017:
018: import java.util.LinkedList;
019: import java.util.Map;
020: import java.util.HashMap;
021: import java.util.ResourceBundle;
022: import java.util.MissingResourceException;
023: import java.util.logging.Level;
024:
025: import java.net.InetAddress;
026: import java.net.UnknownHostException;
027:
028: import java.text.MessageFormat;
029:
030: //JMX
031: import javax.management.ObjectName;
032: import javax.management.MalformedObjectNameException;
033: import javax.management.MBeanServerConnection;
034:
035: //JSF
036: import javax.faces.validator.Validator;
037: import javax.faces.validator.ValidatorException;
038: import javax.faces.context.FacesContext;
039: import javax.faces.el.VariableResolver;
040: import javax.faces.component.UIComponent;
041: import javax.faces.application.FacesMessage;
042: import javax.faces.event.ActionEvent;
043:
044: import com.sun.web.ui.model.FileChooserModel;
045: import com.sun.web.ui.model.ResourceModel;
046:
047: import com.sun.portal.admin.common.util.AdminClientUtil;
048: import com.sun.portal.admin.console.common.PortalBaseBean;
049:
050: /**
051: * @author cathywu
052: */
053: public class ImportExportBaseBean extends PortalBaseBean {
054:
055: public static final String DEFAULT_SCOPE = "2";
056: public static final String RB_NAME = "fabric";
057:
058: protected MBeanServerConnection mbsc;
059: protected Map rbMap;
060: protected String portalId;
061: protected String titleStr = "Export Desktop for";
062: protected String title;
063: protected String cancelText = "Cancel";
064: protected boolean inProgress = false;
065: protected boolean renderParFileName = true;
066: protected String server = null;
067: //protected FileChooserModel model = null;
068: protected Boolean disableImport = null;
069: protected Boolean disableExport = null;
070: protected File lookin = null;
071: protected boolean isLocal = true;
072:
073: //alert member variables
074: protected Boolean displayError;
075: protected String alertSummary;
076: protected String alertDetail;
077: protected String alertType;
078: //protected String errorDetailsTitle;
079: //protected String errorDetailsPageHelp;
080: protected String logMessage;
081:
082: public ImportExportBaseBean() {
083: try {
084: server = InetAddress.getLocalHost().getHostName();
085: } catch (UnknownHostException ukhe) {
086: server = "localhost";
087: }
088: log(Level.INFO, "ImportExportBaseBean.constructor(), server: "
089: + server);
090:
091: String osName = System.getProperty("os.name").toUpperCase();
092: if (osName.startsWith("WINDOW")) {
093: lookin = new File("c:\\\\");
094: } else {
095: lookin = new File("/");
096: }
097:
098: rbMap = getResourceStringMap(RB_NAME);
099: mbsc = getMBeanServerConnection();
100: displayError = Boolean.FALSE;
101: }
102:
103: /*
104: * inProgress methods
105: */
106: public boolean isInProgress() {
107: return inProgress;
108: }
109:
110: /**
111: * renderParFileName methods
112: */
113: public boolean isRenderParFileName() {
114: return renderParFileName;
115: }
116:
117: /**
118: * Title methods
119: */
120: public String getTitle() {
121: setTitle(null);
122:
123: return title;
124: }
125:
126: public void setTitle(String value) {
127: FacesContext ctx = FacesContext.getCurrentInstance();
128: if (getPortalId(ctx) != null) {
129: //setDisplayError(Boolean.FALSE);
130: //setLogMessage(null);
131: portalId = getPortalId(ctx);
132: title = new String(titleStr + " " + portalId);
133: }
134: }
135:
136: /**
137: * cancel methods
138: */
139:
140: public String getCancelText() {
141: return cancelText;
142: }
143:
144: public void setCancelText(String val) {
145: cancelText = val;
146: }
147:
148: /**
149: * disable flags
150: */
151: public boolean isDisableImport() {
152: if (disableImport == null) {
153: disableImport = new Boolean(false);
154: }
155: return disableImport.booleanValue();
156: }
157:
158: public void setDisableImport(boolean val) {
159: disableImport = new Boolean(val);
160: }
161:
162: public boolean isDisableExport() {
163: if (disableExport == null) {
164: disableExport = new Boolean(false);
165: }
166: return disableExport.booleanValue();
167: }
168:
169: public void setDisableExport(boolean val) {
170: disableExport = new Boolean(val);
171: }
172:
173: /**
174: * Lookin
175: */
176: public File getLookin() {
177: return lookin;
178: }
179:
180: /**
181: * validator for parFileName
182: */
183:
184: public void validateParFileName(FacesContext ctx, UIComponent comp,
185: Object value) {
186:
187: String parFileName = (String) value;
188: String sm = (String) rbMap.get("error.validation.summary");
189: String dm = (String) rbMap.get("error.par.file.extension");
190: if (!parFileName.endsWith(".par")) {
191: throw new ValidatorException(new FacesMessage(sm, dm));
192: }
193:
194: File f = new File(parFileName);
195: File parent = f.getParentFile();
196:
197: if (parFileName.indexOf(File.separator) != -1
198: && !parent.exists()) {
199: dm = (String) rbMap.get("error.file.or.dir.not.found")
200: + parent.getAbsolutePath();
201: throw new ValidatorException(new FacesMessage(sm, dm));
202: }
203:
204: if (!f.isAbsolute()) {
205: dm = (String) rbMap.get("error.parfile.full.path.required");
206: throw new ValidatorException(new FacesMessage(sm, dm));
207: }
208:
209: if (this instanceof ImportBean) {
210: String fullPath = f.getAbsolutePath();
211:
212: if (!f.exists()) {
213: dm = (String) rbMap.get("error.file.or.dir.not.found")
214: + fullPath;
215: throw new ValidatorException(new FacesMessage(sm, dm));
216: }
217: } else if (this instanceof ExportBean) {
218: String fullPath = f.getAbsolutePath();
219:
220: if (f.exists()) {
221: dm = (String) rbMap
222: .get("error.par.file.already.exists")
223: + fullPath;
224: throw new ValidatorException(new FacesMessage(sm, dm));
225: }
226: }
227:
228: }
229:
230: /**
231: * File chooser methods
232: */
233:
234: public String getServer() {
235: return server;
236: }
237:
238: /**
239: * If isLocal is false, the file chooser button will not be shown.
240: */
241: public boolean getIsLocal() {
242: //calling isLocal() from PortalBaseBean
243: return isLocal();
244: }
245:
246: /**
247: * Alert methods
248: */
249: public Boolean getDisplayError() {
250: return displayError;
251: }
252:
253: public void setDisplayError(Boolean value) {
254: displayError = value;
255: }
256:
257: public String getAlertSummary() {
258: return alertSummary;
259: }
260:
261: public void setAlertSummary(String summary) {
262: alertSummary = summary;
263: }
264:
265: public String getAlertDetail() {
266: return alertDetail;
267: }
268:
269: public void setAlertDetail(String detail) {
270: alertDetail = detail;
271: }
272:
273: public String getAlertType() {
274: return alertType;
275: }
276:
277: public void setAlertType(String type) {
278: alertType = type;
279: }
280:
281: public String getLogMessage() {
282: return logMessage;
283: }
284:
285: public void setLogMessage(String val) {
286: logMessage = val;
287: }
288:
289: /*
290: public String getErrorDetailsTitle() {
291: return errorDetailsTitle;
292: }
293:
294: public void setErrorDetailsTitle(String val) {
295: errorDetailsTitle = val;
296: }
297:
298: public String getErrorDetailsPageHelp() {
299: return errorDetailsPageHelp;
300: }
301:
302: public void setErrorDetailsPageHelp(String val) {
303: errorDetailsPageHelp = val;
304: }
305: */
306: /**
307: * utility methods
308: */
309:
310: protected ObjectName getMBeanObjectName(String domainId,
311: String portalId) throws Exception {
312: ObjectName objName = null;
313: LinkedList path = new LinkedList();
314: path.addFirst(domainId);
315: path.addFirst(portalId);
316:
317: try {
318: // get component mbean object name
319: String resourceId = "DesktopDynamic";
320: path.addFirst(resourceId);
321: objName = AdminClientUtil.getResourceMBeanObjectName(
322: AdminClientUtil.PORTAL_MBEAN_TYPE + "."
323: + resourceId, path);
324: } catch (MalformedObjectNameException me) {
325: log(
326: Level.SEVERE,
327: "Exception in ImportExportBaseBean.getMBeanObjectName()",
328: me);
329: throw me;
330: }
331: return objName;
332: }
333:
334: protected String getPortalId(FacesContext context) {
335: VariableResolver vr = context.getApplication()
336: .getVariableResolver();
337: String portalId = (String) getSessionAttribute("importExport.portalId");
338:
339: Object obj = vr.resolveVariable(context, "ListPortalsBean");
340: if ((obj != null) && (obj instanceof ListPortalsBean)) {
341: ListPortalsBean lpb = (ListPortalsBean) obj;
342: String pId = lpb.getSelectedPortal();
343:
344: if (pId != null) {
345: setSessionAttribute("importExport.portalId", pId);
346: }
347:
348: }
349:
350: return portalId;
351: }
352:
353: protected String composeLogMessage(Exception e) {
354: StringBuffer msg = new StringBuffer();
355: msg.append(e.getMessage());
356: msg.append('\n');
357: msg.append(getStackTrace(e));
358: return msg.toString();
359: }
360:
361: protected void setupAlert(Object[] tokens, String summary,
362: String detail, String type, String message) {
363:
364: String sm = (String) rbMap.get(summary);
365: String dm = (String) rbMap.get(detail);
366:
367: if (tokens != null) {
368: String pattern = dm;
369: MessageFormat mf = new MessageFormat(pattern);
370: dm = mf.format(tokens);
371: }
372: if (message != null) {
373: dm = dm
374: + " "
375: + (String) rbMap
376: .get("error.importexport.viewSource");
377: setLogMessage(message);
378: } else {
379: setLogMessage("");
380: }
381:
382: setDisplayError(Boolean.TRUE);
383: setAlertSummary(sm);
384: setAlertDetail(dm);
385: setAlertType(type);
386: }
387:
388: /*
389: * CreateThread class
390: *
391: * This class creates a thread for progress image to be dislayed
392: */
393:
394: class CreateThread extends Thread {
395: ImportExportBaseBean bean = null;
396: boolean doImport = true;
397:
398: CreateThread(ImportExportBaseBean bean, boolean doImport) {
399: this .bean = bean;
400: this .doImport = doImport;
401: }
402:
403: public void run() {
404: if (doImport) {
405: ((ImportBean) bean).doImport(this );
406: } else {
407: ((ExportBean) bean).doExport(this );
408: }
409:
410: inProgress = false;
411: }
412: }
413:
414: }
|