Java tutorial
/** * This file is part of tera-api. * * tera-api is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * tera-api is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with tera-api. If not, see <http://www.gnu.org/licenses/>. */ package com.tera.common.vcontext.service; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Collection; import java.util.List; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.NameFileFilter; import org.osgi.framework.Bundle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.tera.common.util.JaxbUtil; import com.tera.common.vcontext.template.VContextTemplate; import com.tera.common.vcontext.template.VElementTemplate; /** * <p> * This class is responsible for parsing vcontext.xml file's content and * updating virtual's context nodes based on specified type * </p> * * @author ATracer * @since 0.0.9 */ public final class ContextConfigurationParser { private static final Logger log = LoggerFactory.getLogger(ContextConfigurationParser.class); /** * @param bundle bundle of the loading resource * @param xsdSchema schema location of vcontext.xml * @param fileSystemXml resource to be loaded */ public static final void parseVirtualConfiguration(Bundle bundle, URL xsdSchema, URL fileSystemXml) { Unmarshaller un = JaxbUtil.create(VContextTemplate.class, xsdSchema); try { VContextTemplate context = (VContextTemplate) un.unmarshal(fileSystemXml.openStream()); List<VElementTemplate> elements = context.getTemplates(); for (VElementTemplate template : elements) { String name = template.getName(); String path = template.getPath(); String target = template.getTarget(); switch (template.getType()) { case DM: ContextRegisterService.registerDM(bundle.getSymbolicName(), name); break; case CONFIG: ContextRegisterService.registerConfig(bundle.getSymbolicName(), name); break; case FILE: ContextRegisterService.registerFile(bundle.getResource(path), name, target); break; case PROPERTIES: ContextRegisterService.registerProperties(bundle.getResource(name), name); break; case COMMAND: ContextRegisterService.registerCommands(bundle.getSymbolicName(), bundle.getEntryPaths(path)); break; case SCHEMA_VERSION: ContextRegisterService.registerSchemaVersions(bundle.getResource(path)); break; } } } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param bundle com.tera.common.vcontext bundle * @param directory top directory with filesystem data */ public static final void parseConfigurationDirectory(Bundle bundle, String directory) { Unmarshaller un = JaxbUtil.create(VContextTemplate.class, bundle.getResource("vcontext.xsd")); File file = new File(directory); if (file.isDirectory()) { File[] contextDirs = file.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY); log.info("Found {} filesystem directories", contextDirs.length); for (File dir : contextDirs) { Collection<File> contextFiles = FileUtils.listFiles(dir, new NameFileFilter("vcontext.xml"), null); for (File contextFile : contextFiles) { try { VContextTemplate context = (VContextTemplate) un .unmarshal(contextFile.toURI().toURL().openStream()); parseRealConfiguration(dir.getAbsolutePath(), context); } catch (Exception e) { log.error("Exception when parsing context resource {}", contextFile.getName(), e); } } } } else { log.warn("Specified context directory doesn't exists {}", directory); } } /** * @param directory absolute path to specific context directory * @param context VContextTemplate that configures loading of the resources in * directory {@code directory} * @throws MalformedURLException */ static void parseRealConfiguration(String directory, VContextTemplate context) throws MalformedURLException { List<VElementTemplate> elements = context.getTemplates(); for (VElementTemplate template : elements) { String name = template.getName(); String path = directory + "/" + template.getPath(); String target = template.getTarget(); switch (template.getType()) { case FILE: ContextRegisterService.registerFile(new File(path).toURI().toURL(), name, target); break; case PROPERTIES: ContextRegisterService.registerProperties(new File(path).toURI().toURL(), name); break; default: throw new UnsupportedOperationException("Cannot load the following type: " + template.getType()); } } } }