ohrstrom@1504: /*
ohrstrom@1504: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504: *
ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504: *
ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504: * accompanied this code).
ohrstrom@1504: *
ohrstrom@1504: * You should have received a copy of the GNU General Public License version
ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504: *
ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504: * questions.
ohrstrom@1504: */
ohrstrom@1504:
ohrstrom@1504: package com.sun.tools.sjavac;
ohrstrom@1504:
ohrstrom@1504: import java.io.*;
ohrstrom@1504: import java.net.URI;
ohrstrom@1504: import java.util.ArrayList;
ohrstrom@1504: import java.util.Collections;
ohrstrom@1504: import java.util.Iterator;
ohrstrom@1504: import java.util.List;
ohrstrom@1504: import java.util.Set;
ohrstrom@1504: import java.util.HashSet;
ohrstrom@1504: import java.util.Map;
ohrstrom@1504: import java.util.Properties;
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * The clean properties transform should not be necessary.
ohrstrom@1504: * Eventually we will cleanup the property file sources in the OpenJDK instead.
ohrstrom@1504: *
ohrstrom@1504: *
This is NOT part of any supported API.
ohrstrom@1504: * If you write code that depends on this, you do so at your own
ohrstrom@1504: * risk. This code and its internal interfaces are subject to change
ohrstrom@1504: * or deletion without notice.
ohrstrom@1504: */
ohrstrom@1504: public class CleanProperties implements Transformer
ohrstrom@1504: {
ohrstrom@1504: public void setExtra(String e) {
ohrstrom@1504: // Any extra information is ignored for clean properties.
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: public void setExtra(String[] a) {
ohrstrom@1504: // Any extra information is ignored for clean properties.
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: public boolean transform(Map> pkgSrcs,
ohrstrom@1504: Set visibleSrcs,
ohrstrom@1504: Map> visibleClasses,
ohrstrom@1504: Map> oldPackageDependencies,
ohrstrom@1504: URI destRoot,
ohrstrom@1504: Map> packageArtifacts,
ohrstrom@1504: Map> packageDependencies,
ohrstrom@1504: Map packagePublicApis,
ohrstrom@1504: int debugLevel,
ohrstrom@1504: boolean incremental,
ohrstrom@1504: int numCores,
ohrstrom@1504: PrintStream out,
ohrstrom@1504: PrintStream err)
ohrstrom@1504: {
ohrstrom@1504: boolean rc = true;
ohrstrom@1504: for (String pkgName : pkgSrcs.keySet()) {
ohrstrom@1504: String pkgNameF = pkgName.replace('.',File.separatorChar);
ohrstrom@1504: for (URI u : pkgSrcs.get(pkgName)) {
ohrstrom@1504: File src = new File(u);
ohrstrom@1504: boolean r = clean(pkgName, pkgNameF, src, new File(destRoot), debugLevel,
ohrstrom@1504: packageArtifacts);
ohrstrom@1504: if (r == false) {
ohrstrom@1504: rc = false;
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: return rc;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: boolean clean(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel,
ohrstrom@1504: Map> packageArtifacts)
ohrstrom@1504: {
ohrstrom@1504: // Load the properties file.
ohrstrom@1504: Properties p = new Properties();
ohrstrom@1504: try {
ohrstrom@1504: p.load(new FileInputStream(src));
ohrstrom@1504: } catch (IOException e) {
ohrstrom@1504: Log.error("Error reading file "+src.getPath());
ohrstrom@1504: return false;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: // Sort the properties in increasing key order.
ohrstrom@1504: List sortedKeys = new ArrayList();
ohrstrom@1504: for (Object key : p.keySet()) {
ohrstrom@1504: sortedKeys.add((String)key);
ohrstrom@1504: }
ohrstrom@1504: Collections.sort(sortedKeys);
ohrstrom@1504: Iterator keys = sortedKeys.iterator();
ohrstrom@1504:
ohrstrom@1504: // Collect the properties into a string buffer.
ohrstrom@1504: StringBuilder data = new StringBuilder();
ohrstrom@1504: while (keys.hasNext()) {
ohrstrom@1504: String key = keys.next();
ohrstrom@1504: data.append(CompileProperties.escape(key)+":"+CompileProperties.escape((String)p.get(key))+"\n");
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
ohrstrom@1504: File dest = new File(destFilename);
ohrstrom@1504:
ohrstrom@1504: // Make sure the dest directories exist.
ohrstrom@1504: if (!dest.getParentFile().isDirectory()) {
ohrstrom@1504: if (!dest.getParentFile().mkdirs()) {
ohrstrom@1504: Log.error("Could not create the directory "+dest.getParentFile().getPath());
ohrstrom@1504: return false;
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: Set as = packageArtifacts.get(pkgName);
ohrstrom@1504: if (as == null) {
ohrstrom@1504: as = new HashSet();
ohrstrom@1504: packageArtifacts.put(pkgName, as);
ohrstrom@1504: }
ohrstrom@1504: as.add(dest.toURI());
ohrstrom@1504:
ohrstrom@1504: if (dest.exists() && dest.lastModified() > src.lastModified()) {
ohrstrom@1504: // A cleaned property file exists, and its timestamp is newer than the source.
ohrstrom@1504: // Assume that we do not need to clean!
ohrstrom@1504: // Thus we are done.
ohrstrom@1504: return true;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: Log.info("Cleaning property file "+pkgNameF+File.separator+src.getName());
ohrstrom@1504: try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {
ohrstrom@1504: writer.write(data.toString());
ohrstrom@1504: } catch ( IOException e ) {
ohrstrom@1504: Log.error("Could not write file "+dest.getPath());
ohrstrom@1504: return false;
ohrstrom@1504: }
ohrstrom@1504: return true;
ohrstrom@1504: }
ohrstrom@1504: }