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.*;
  29 import java.net.URI;
  30 import java.util.Set;
  31 import java.util.HashSet;
  32 import java.util.Map;
  33 
  34 import com.sun.tools.sjavac.options.Options;
  35 import com.sun.tools.sjavac.server.Sjavac;
  36 
  37 /**
  38  * The copy file transform simply copies a matching file from -src to -d .
  39  * Such files are typically images, xml documents and other data files.
  40  *
  41  *  <p><b>This is NOT part of any supported API.
  42  *  If you write code that depends on this, you do so at your own risk.
  43  *  This code and its internal interfaces are subject to change or
  44  *  deletion without notice.</b>
  45  */
  46 public class CopyFile implements Transformer {
  47 
  48     public void setExtra(String e) {
  49     }
  50 
  51     public void setExtra(Options a) {
  52     }
  53 
  54     public boolean transform(Sjavac sjavac,
  55                              Map<String,Set<URI>> pkgSrcs,
  56                              Set<URI> visibleSrcs,
  57                              Map<URI,Set<String>> visibleClasses,
  58                              Map<String,Set<String>> oldPackageDependents,
  59                              URI destRoot,
  60                              Map<String,Set<URI>>    packageArtifacts,
  61                              Map<String,Set<String>> packageDependencies,
  62                              Map<String,String>      packagePubapis,
  63                              int debugLevel,
  64                              boolean incremental,
  65                              int numCores,
  66                              PrintStream out,
  67                              PrintStream err)
  68     {
  69         boolean rc = true;
  70         String dest_filename;
  71         File dest;
  72 
  73         for (String pkgName : pkgSrcs.keySet()) {
  74             String pkgNameF = Util.toFileSystemPath(pkgName);
  75             for (URI u : pkgSrcs.get(pkgName)) {
  76                 File src = new File(u);
  77                 File destDir;
  78                 destDir = new File(destRoot.getPath()+File.separator+pkgNameF);
  79                 dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
  80                 dest = new File(dest_filename);
  81 
  82                 if (!destDir.isDirectory()) {
  83                     if (!destDir.mkdirs()) {
  84                        Log.error("Error: The copier could not create the directory "+
  85                                            destDir.getPath());
  86                         return false;
  87                     }
  88                 }
  89 
  90                 Set<URI> as = packageArtifacts.get(pkgName);
  91                 if (as == null) {
  92                     as = new HashSet<>();
  93                     packageArtifacts.put(pkgName, as);
  94                 }
  95                 as.add(dest.toURI());
  96 
  97                 if (dest.exists() && dest.lastModified() > src.lastModified()) {
  98                     // A copied file exists, and its timestamp is newer than the source.
  99                     continue;
 100                 }
 101 
 102                 Log.info("Copying "+pkgNameF+File.separator+src.getName());
 103 
 104                 try (InputStream fin = new FileInputStream(src);
 105                      OutputStream fout = new FileOutputStream(dest)) {
 106                     byte[] buf = new byte[1024];
 107                     int len;
 108                     while ((len = fin.read(buf)) > 0){
 109                         fout.write(buf, 0, len);
 110                     }
 111                 }
 112                 catch(IOException e){
 113                     Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());
 114                     rc = false;
 115                 }
 116             }
 117         }
 118         return rc;
 119     }
 120 }