All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.reficio.p2.utils.JarUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2012 Reficio (TM) - Reestablish your software! All Rights Reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.reficio.p2.utils;


import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.FileResource;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Resource;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.logging.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * @author Tom Bujok ([email protected])
* Reficio (TM) - Reestablish your software!
* http://www.reficio.org * @since 1.0.0 */ public class JarUtils { public static void adjustSnapshotOutputVersion(File inputFile, File outputFile, String version) { Jar jar = null; try { jar = new Jar(inputFile); Manifest manifest = jar.getManifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue(Analyzer.BUNDLE_VERSION, version); jar.write(outputFile); } catch (Exception e) { throw new RuntimeException("Cannot open jar " + outputFile, e); } finally { if (jar != null) { jar.close(); } } } /** * Opens the feature.xml in the given jar file and adjusts all version numbers/timestamps * * @param inputFile - inputFile * @param outputFile - outputFile * @param log - log * @param pluginDir - pluginDir * @param timestamp - timestamp * */ @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") public static void adjustFeatureXml(File inputFile, File outputFile, File pluginDir, Log log, String timestamp) { Jar jar = null; File newXml = null; try { jar = new Jar(inputFile); Resource res = jar.getResource("feature.xml"); Document featureSpec = XmlUtils.parseXml(res.openInputStream()); adjustFeatureQualifierVersionWithTimestamp(featureSpec, timestamp); adjustFeaturePluginData(featureSpec, pluginDir, log); File temp = new File(outputFile.getParentFile(),"temp"); temp.mkdir(); newXml = new File(temp,"feature.xml"); XmlUtils.writeXml(featureSpec, newXml); FileResource newRes = new FileResource(newXml); jar.putResource("feature.xml", newRes, true); jar.write(outputFile); } catch (Exception e) { throw new RuntimeException("Cannot open jar " + outputFile, e); } finally { if (jar != null) { jar.close(); } if (newXml != null) { newXml.delete(); } } } public static void adjustFeatureQualifierVersionWithTimestamp(Document featureSpec, String timestamp) { String version = featureSpec.getDocumentElement().getAttributeNode("version").getValue(); String newVersion = Utils.eclipseQualifierToTimeStamp(version, timestamp); featureSpec.getDocumentElement().getAttributeNode("version").setValue(newVersion); } static Comparator fileComparator = new Comparator() { @Override public int compare(File arg0, File arg1) { return arg0.getName().compareTo(arg1.getName()); } }; /** * Adjust the pluginId TODO - this may be wrong if singleton is used * * @param pluginDir - pluginDir * @param featureSpec - featureSpec * @param log - log * * @throws IOException - an exception */ public static void adjustFeaturePluginData(Document featureSpec, File pluginDir, Log log) throws IOException { //get list of all plugins NodeList plugins = featureSpec.getElementsByTagName("plugin"); for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy