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: * @(#)ArchiveHelper.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * ValidatorHelper.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on January 2, 2006, 4:49 PM
037: */package com.sun.jbi.management.util;
038:
039: import com.sun.jbi.StringTranslator;
040: import com.sun.jbi.management.LocalStringKeys;
041: import com.sun.jbi.management.descriptor.Jbi;
042: import com.sun.jbi.management.message.MessageBuilder;
043:
044: import java.io.File;
045: import java.io.InputStream;
046: import java.math.BigInteger;
047: import java.util.zip.ZipEntry;
048: import java.util.zip.ZipFile;
049: import java.util.zip.ZipInputStream;
050:
051: import javax.jbi.JBIException;
052: import javax.xml.bind.JAXBContext;
053: import javax.xml.bind.Unmarshaller;
054: import javax.xml.bind.Marshaller;
055:
056: import com.sun.jbi.management.system.ManagementContext;
057:
058: /**
059: * Helper class for validators
060: *
061: * @author Sun Microsystems, Inc
062: */
063: public class ArchiveHelper {
064: private static final String JBI_XML = "jbi.xml";
065: private static final String JBI_XML_PATH = "META-INF/" + JBI_XML;
066:
067: /**
068: * registry schema file
069: */
070: public static final String JBI_DESCRIPTOR_SCHEMA = "jbi.xsd";
071:
072: /**
073: * registry schema subdir in JBI_HOME
074: */
075: public static final String JBI_DESCRIPTOR_SCHEMA_DIR = "schemas";
076:
077: private Jbi mJbiXml;
078: private Unmarshaller mReader;
079:
080: private StringTranslator mTranslator;
081: private MessageBuilder mMsgBuilder;
082:
083: /**
084: * descriptor schema path
085: */
086: private File mDescSchema;
087:
088: /**
089: */
090: public ArchiveHelper(com.sun.jbi.EnvironmentContext envCtx)
091: throws JBIException
092:
093: {
094: // setup JAXB
095: try {
096: mTranslator = envCtx
097: .getStringTranslator("com.sun.jbi.management");
098: mMsgBuilder = new MessageBuilder(mTranslator);
099: JAXBContext jc = JAXBContext.newInstance(
100: "com.sun.jbi.management.descriptor", Class.forName(
101: "com.sun.jbi.management.descriptor.Jbi")
102: .getClassLoader());
103: mReader = jc.createUnmarshaller();
104:
105: File schemaDir = new File(envCtx.getJbiInstallRoot(),
106: JBI_DESCRIPTOR_SCHEMA_DIR);
107: mDescSchema = new File(schemaDir, JBI_DESCRIPTOR_SCHEMA);
108:
109: } catch (Exception ex) {
110:
111: String exMsg = ex.getMessage();
112: if (mMsgBuilder != null) {
113: exMsg = mMsgBuilder.buildExceptionMessage(
114: "ArchiveHelper<init>", ex);
115: }
116: throw new JBIException(exMsg);
117: }
118:
119: }
120:
121: /**
122: * Load the jbi.xml from the archive
123: */
124: public Jbi loadJbiXml(File archiveFile, boolean validate)
125: throws JBIException {
126: archiveExistenceCheck(archiveFile);
127:
128: ZipFile zip = null;
129: try {
130: zip = new ZipFile(archiveFile);
131: ZipEntry jbiXmlEntry = zip.getEntry(JBI_XML_PATH);
132:
133: // process JBI metadata
134: if (jbiXmlEntry == null) {
135: String[] params = new String[] {
136: archiveFile.getAbsolutePath(), JBI_XML_PATH };
137: String errMsg = mTranslator
138: .getString(
139: LocalStringKeys.JBI_ADMIN_NO_DESCRIPTOR_IN_ARCHIVE,
140: params);
141:
142: String jbiMsg = mMsgBuilder.buildFrameworkMessage(
143: "loadJbiXml", MessageBuilder.TaskResult.FAILED,
144: MessageBuilder.MessageType.ERROR, mMsgBuilder
145: .getMessageString(errMsg), params,
146: mMsgBuilder.getMessageToken(errMsg));
147: throw new javax.jbi.JBIException(jbiMsg);
148: }
149:
150: if (validate) {
151: mReader
152: .setSchema(javax.xml.validation.SchemaFactory
153: .newInstance(
154: javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)
155: .newSchema(mDescSchema));
156: }
157:
158: mJbiXml = loadJbiXml(archiveFile.getName(), zip
159: .getInputStream(jbiXmlEntry));
160: } catch (java.io.IOException ex) {
161: String exMsg = mMsgBuilder.buildExceptionMessage(
162: "loadJbiXml", ex);
163: throw new JBIException(exMsg);
164: } catch (org.xml.sax.SAXException ex) {
165: String exMsg = mMsgBuilder.buildExceptionMessage(
166: "loadJbiXml", ex);
167: throw new JBIException(exMsg);
168: } finally {
169: if (zip != null) {
170: try {
171: zip.close();
172: } catch (java.io.IOException ioex) {
173: String exMsg = mMsgBuilder.buildExceptionMessage(
174: "loadJbiXml", ioex);
175: throw new JBIException(exMsg);
176: }
177: }
178: }
179: return mJbiXml;
180: }
181:
182: /**
183: * @return the JAXB jbi type for jbi.xml from the input stream
184: */
185: private Jbi loadJbiXml(String entryName, InputStream jbiXmlStream)
186: throws JBIException {
187: Jbi jbiXml;
188:
189: try {
190: jbiXml = (Jbi) mReader.unmarshal(jbiXmlStream);
191:
192: } catch (javax.xml.bind.JAXBException jEx) {
193: String message = jEx.getLinkedException() != null ? jEx
194: .getLinkedException().getMessage() : jEx
195: .getMessage();
196: if (message == null) {
197: message = jEx.toString();
198: }
199:
200: // -- schema validation failed
201: String[] params = new String[] { entryName, message };
202: String errMsg = mTranslator
203: .getString(
204: LocalStringKeys.JBI_ADMIN_ARCHIVE_DESCRIPTOR_NOT_SCHEMA_VALID,
205: params);
206: String jbiMsg = mMsgBuilder.buildFrameworkMessage(
207: "loadJbiXml", MessageBuilder.TaskResult.FAILED,
208: MessageBuilder.MessageType.ERROR, mMsgBuilder
209: .getMessageString(errMsg), params,
210: mMsgBuilder.getMessageToken(errMsg));
211:
212: throw new JBIException(jbiMsg);
213: }
214: return jbiXml;
215: }
216:
217: /**
218: * @throws a JBIException if the archive file does not exist or is a zero length file.
219: */
220: private void archiveExistenceCheck(File archiveFile)
221: throws javax.jbi.JBIException {
222: if (!archiveFile.exists()) {
223: String[] params = new String[] { archiveFile
224: .getAbsolutePath() };
225: String errMsg = mTranslator.getString(
226: LocalStringKeys.JBI_ADMIN_ARCHIVE_NONEXISTENT,
227: params);
228:
229: String jbiMsg = mMsgBuilder.buildFrameworkMessage(
230: "loadJbiXml", MessageBuilder.TaskResult.FAILED,
231: MessageBuilder.MessageType.ERROR, mMsgBuilder
232: .getMessageString(errMsg), params,
233: mMsgBuilder.getMessageToken(errMsg));
234: throw new javax.jbi.JBIException(jbiMsg);
235: }
236:
237: if (archiveFile.length() == 0) {
238: String[] params = new String[] { archiveFile
239: .getAbsolutePath() };
240: String errMsg = mTranslator.getString(
241: LocalStringKeys.JBI_ADMIN_ARCHIVE_EMPTY, params);
242:
243: String jbiMsg = mMsgBuilder.buildFrameworkMessage(
244: "loadJbiXml", MessageBuilder.TaskResult.FAILED,
245: MessageBuilder.MessageType.ERROR, mMsgBuilder
246: .getMessageString(errMsg), params,
247: mMsgBuilder.getMessageToken(errMsg));
248: throw new javax.jbi.JBIException(jbiMsg);
249: }
250: }
251: }
|