/** * Read in a Features from the input stream. * * @param uri uri to read * @param validate whether to validate the input. * @return a Features read from the input stream */ public static Features unmarshal(String uri, boolean validate) { return unmarshal(uri, null, validate); }
private void marshalStorageFile(File storage, Features features) throws Exception { OutputStream storageFileOs = new FileOutputStream(storage); JaxbUtil.marshal(features, storageFileOs); storageFileOs.close(); } }
private static void fixDom(Document doc, Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (!FeaturesNamespaces.URI_CURRENT.equals(node.getNamespaceURI())) { doc.renameNode(node, FeaturesNamespaces.URI_CURRENT, node.getLocalName()); } NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { fixDom(doc, children.item(i)); } } }
public static Features unmarshal(String uri, InputStream stream, boolean validate) { Features features; if (validate) { features = unmarshalValidate(uri, stream); } else { features = unmarshalNoValidate(uri, stream); } features.postUnmarshall(uri); return features; }
LOGGER.warn("Old style feature file without namespace found (URI: {}). This format is deprecated and support for it will soon be removed", uri); } else { Schema schema = getSchema(nsuri); try { schema.newValidator().validate(new DOMSource(doc)); fixDom(doc, doc.getDocumentElement()); Unmarshaller unmarshaller = FEATURES_CONTEXT.createUnmarshaller(); Features features = (Features) unmarshaller.unmarshal(new DOMSource(doc));
protected Features getFeatures(final URL repoUrl) throws JAXBException, IOException { return JaxbUtil.unmarshal(repoUrl.toExternalForm(), false); }
private static void writeDependencies(Features features, File file) throws JAXBException, IOException { file.getParentFile().mkdirs(); if (!file.getParentFile().exists() || !file.getParentFile().isDirectory()) { throw new IOException("Cannot create directory at " + file.getParent()); } try (OutputStream out = new FileOutputStream(file)) { JaxbUtil.marshal(features, out); } }
static Features readFeaturesFile(File featuresFile) throws XMLStreamException, JAXBException, IOException { return JaxbUtil.unmarshal(featuresFile.toURI().toASCIIString(), false); }
private static void writeDependencies(Features features, File file) throws JAXBException, IOException { file.getParentFile().mkdirs(); if (!file.getParentFile().exists() || !file.getParentFile().isDirectory()) { throw new IOException("Cannot create directory at " + file.getParent()); } try (OutputStream out = new FileOutputStream(file)) { JaxbUtil.marshal(features, out); } }
static Features readFeaturesFile(File featuresFile) throws XMLStreamException, JAXBException, IOException { return JaxbUtil.unmarshal(featuresFile.toURI().toASCIIString(), false); }
@Override public String getFeatureXml(Feature feature) { try { StringWriter sw = new StringWriter(); Features r = new Features(); r.getFeature().add((org.apache.karaf.features.internal.model.Feature) feature); JaxbUtil.marshal(r, sw); String[] strs = sw.toString().split("\n"); StringJoiner joiner = new StringJoiner("\n"); for (int i = 2; i < strs.length - 1; i++) { joiner.add(strs[i]); } return joiner.toString(); } catch (JAXBException e) { return null; } }
private void load(boolean validate) { if (features == null) { try (InputStream inputStream = new InterruptibleInputStream(uri.toURL().openStream())) { features = JaxbUtil.unmarshal(uri.toASCIIString(), inputStream, validate); } catch (Exception e) { throw new RuntimeException(e.getMessage() + " : " + uri, e); } } }
public void execute() throws MojoExecutionException, MojoFailureException { Set<Feature> featuresSet = resolveFeatures(); if (mergedFeature) { Feature feature = oneVersion ? mergeFeatureOneVersion(featuresSet) : mergeFeature(featuresSet); featuresSet = new HashSet<>(); featuresSet.add(feature); } try { metaDataFile.getParentFile().mkdirs(); Features features = new Features(); features.getFeature().addAll(featuresSet); try (OutputStream os = new FileOutputStream(metaDataFile)) { JaxbUtil.marshal(features, os); } } catch (Exception e) { throw new RuntimeException("Error writing feature meta data to " + metaDataFile + ": " + e.getMessage(), e); } }
@Override public List<String> list() throws Exception { List<String> repositories = new ArrayList<>(); File storage = new File(STORAGE); if (storage.exists()) { Features features = JaxbUtil.unmarshal(storage.getAbsolutePath(), true); repositories = features.getRepository(); } return repositories; }
@Override public Feature[] repositoryProvidedFeatures(URI uri) throws Exception { Features features = JaxbUtil.unmarshal(uri.toURL().toExternalForm(), true); Feature[] array = new Feature[features.getFeature().size()]; return features.getFeature().toArray(array); }
@Override public void remove(String url) throws Exception { File storage = new File(STORAGE); if (!storage.exists()) { return; } Features features = JaxbUtil.unmarshal(storage.getAbsolutePath(), true); if (!isFeaturesRepositoryRegistered(url, features.getRepository())) { throw new IllegalArgumentException("Features repository " + url + " is not registered in the gateway"); } features.getRepository().remove(url); marshalStorageFile(storage, features); }
public void execute() throws MojoExecutionException, MojoFailureException { Set<Feature> featuresSet = resolveFeatures(); if (mergedFeature) { Feature feature = oneVersion ? mergeFeatureOneVersion(featuresSet) : mergeFeature(featuresSet); featuresSet = new HashSet<>(); featuresSet.add(feature); } try { metaDataFile.getParentFile().mkdirs(); Features features = new Features(); features.getFeature().addAll(featuresSet); try (OutputStream os = new FileOutputStream(metaDataFile)) { JaxbUtil.marshal(features, os); } } catch (Exception e) { throw new RuntimeException("Error writing feature meta data to " + metaDataFile + ": " + e.getMessage(), e); } }
@Override public void register(String url) throws Exception { File storage = new File(STORAGE); Features features = new Features(); if (storage.exists()) { features = JaxbUtil.unmarshal(storage.getAbsolutePath(), true); } else { storage.getParentFile().mkdirs(); storage.createNewFile(); } features.setName("cave-gateway"); if (isFeaturesRepositoryRegistered(url, features.getRepository())) { throw new IllegalArgumentException("Features repository " + url + " already registered in the gateway"); } features.getRepository().add(url); marshalStorageFile(storage, features); }