001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037:
038: package oracle.toplink.essentials.internal.weaving;
039:
040: import java.io.File;
041: import java.io.FileInputStream;
042: import java.io.FileOutputStream;
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.net.URISyntaxException;
046: import java.net.URL;
047: import java.util.jar.JarEntry;
048:
049: /**
050: * The class provides a set of methods to pack passed-in entries into the sepcified archive file.
051: * the class handle directory output.
052: */
053: public class StaticWeaveDirectoryOutputHandler extends
054: AbstractStaticWeaveOutputHandler {
055: private URL source = null;
056: private URL target = null;
057:
058: /**
059: * Construct an instance of StaticWeaveDirectoryOutputHandler.
060: * @param source
061: * @param target
062: */
063: public StaticWeaveDirectoryOutputHandler(URL source, URL target) {
064: this .source = source;
065: this .target = target;
066: }
067:
068: /**
069: * create directory into target directory.
070: * @param dirPath
071: * @throws IOException
072: */
073: public void addDirEntry(String dirPath) throws IOException {
074: File file = new File(this .target.getPath() + File.separator
075: + dirPath).getAbsoluteFile();
076: if (!file.exists()) {
077: file.mkdirs();
078: }
079: }
080:
081: /**
082: * Write entry bytes into target, this method is usually invoked if class has been tranformed
083: * @param targetEntry
084: * @param entryBytes
085: * @throws IOException
086: */
087: public void addEntry(JarEntry targetEntry, byte[] entryBytes)
088: throws IOException {
089: File target = new File(this .target.getPath()
090: + targetEntry.getName()).getAbsoluteFile();
091: if (!target.exists()) {
092: target.createNewFile();
093: }
094: (new FileOutputStream(target)).write(entryBytes);
095: }
096:
097: /**
098: * Write entry into target, this method usually copy original class into target.
099: * @param jis
100: * @param entry
101: * @throws IOException
102: */
103: public void addEntry(InputStream jis, JarEntry entry)
104: throws IOException, URISyntaxException {
105: File target = new File(this .target.getPath() + entry.getName())
106: .getAbsoluteFile();
107: if (!target.exists()) {
108: target.createNewFile();
109: }
110: if ((new File(this .source.toURI())).isDirectory()) {
111: File sourceEntry = new File(this .source.getPath()
112: + entry.getName());
113: FileInputStream fis = new FileInputStream(sourceEntry);
114: byte[] classBytes = new byte[fis.available()];
115: fis.read(classBytes);
116: (new FileOutputStream(target)).write(classBytes);
117: } else {
118: readwriteStreams(jis, (new FileOutputStream(target)));
119: }
120: }
121: }
|