1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.sjavac; 27 28 import java.io.File; 29 import java.io.FileInputStream; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.OutputStream; 34 import java.io.PrintStream; 35 import java.net.URI; 36 import java.util.HashSet; 37 import java.util.Map; 38 import java.util.Set; 39 40 import com.sun.tools.sjavac.options.Options; 41 import com.sun.tools.sjavac.pubapi.PubApi; 42 import com.sun.tools.sjavac.server.Sjavac; 43 44 /** 45 * The copy file transform simply copies a matching file from -src to -d . 46 * Such files are typically images, xml documents and other data files. 47 * 48 * <p><b>This is NOT part of any supported API. 49 * If you write code that depends on this, you do so at your own risk. 50 * This code and its internal interfaces are subject to change or 51 * deletion without notice.</b> 52 */ 53 public class CopyFile implements Transformer { 54 55 public void setExtra(String e) { 56 } 57 58 public void setExtra(Options a) { 59 } 60 61 public boolean transform(Sjavac sjavac, 62 Map<String,Set<URI>> pkgSrcs, 63 Set<URI> visibleSrcs, 64 Map<URI,Set<String>> visibleClasses, 65 Map<String,Set<String>> oldPackageDependents, 66 URI destRoot, 67 Map<String,Set<URI>> packageArtifacts, 68 Map<String,Map<String, Set<String>>> packageDependencies, 69 Map<String,Map<String, Set<String>>> packageCpDependencies, 70 Map<String, PubApi> packagePubapis, 71 Map<String, PubApi> dependencyPubapis, 72 int debugLevel, 73 boolean incremental, 74 int numCores, 75 PrintStream out, 76 PrintStream err) 77 { 78 boolean rc = true; 79 String dest_filename; 80 File dest; 81 82 for (String pkgName : pkgSrcs.keySet()) { 83 String pkgNameF = Util.toFileSystemPath(pkgName); 84 for (URI u : pkgSrcs.get(pkgName)) { 85 File src = new File(u); 86 File destDir; 87 destDir = new File(destRoot.getPath()+File.separator+pkgNameF); 88 dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName(); 89 dest = new File(dest_filename); 90 91 if (!destDir.isDirectory()) { 92 if (!destDir.mkdirs()) { 93 Log.error("Error: The copier could not create the directory "+ 94 destDir.getPath()); 95 return false; 96 } 97 } 98 99 Set<URI> as = packageArtifacts.get(pkgName); 100 if (as == null) { 101 as = new HashSet<>(); 102 packageArtifacts.put(pkgName, as); 103 } 104 as.add(dest.toURI()); 105 106 if (dest.exists() && dest.lastModified() > src.lastModified()) { 107 // A copied file exists, and its timestamp is newer than the source. 108 continue; 109 } 110 111 Log.info("Copying "+pkgNameF+File.separator+src.getName()); 112 113 try (InputStream fin = new FileInputStream(src); 114 OutputStream fout = new FileOutputStream(dest)) { 115 byte[] buf = new byte[1024]; 116 int len; 117 while ((len = fin.read(buf)) > 0){ 118 fout.write(buf, 0, len); 119 } 120 } 121 catch(IOException e){ 122 Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath()); 123 rc = false; 124 } 125 } 126 } 127 return rc; 128 } 129 }