org.ow2.util.archive.impl.DefaultArchiveFactory Maven / Gradle / Ivy
/**
* Copyright 2007-2012 Bull S.A.S.
*
* Licensed 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.ow2.util.archive.impl;
import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
import org.ow2.util.archive.api.IArchive;
import org.ow2.util.archive.api.IArchiveFactory;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;
/**
* Default factory that manages the file (jar file or directories).
* @author Florent Benoit
*/
public class DefaultArchiveFactory implements IArchiveFactory {
/**
* Logger.
*/
private Log logger = LogFactory.getLog(DefaultArchiveFactory.class);
/**
* Creates an EZBArchive object for the given object.
* @param file the file that this factory can manage.
* @return a new EZBArchive instance or null if it's not an archive
*/
public IArchive create(final File file) {
if (file.isDirectory()) {
return new DirectoryArchiveImpl(file);
}
// check that this is a valid JAR file, else return null
JarFile jarFile = null;
try {
jarFile = new JarFile(file);
} catch (IOException e) {
// not a valid jar file
logger.debug("File ''{0}'' is not a valid Jar File", file);
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
logger.debug("Cannot close the jar file ''{0}''", jarFile);
}
}
}
if (jarFile != null) {
return new JarArchiveImpl(file);
} else {
return new FileArchiveImpl(file);
}
}
/**
* @return the type supported by the factory
*/
public Class getSupportedClass() {
return File.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy