Search in sources :

Example 1 with KarafPropertyInstructionsModelStaxReader

use of org.apache.karaf.tools.utils.model.io.stax.KarafPropertyInstructionsModelStaxReader in project karaf by apache.

the class AssemblyMojo method doExecute.

protected void doExecute() throws Exception {
    startupRepositories = nonNullList(startupRepositories);
    bootRepositories = nonNullList(bootRepositories);
    installedRepositories = nonNullList(installedRepositories);
    startupBundles = nonNullList(startupBundles);
    bootBundles = nonNullList(bootBundles);
    installedBundles = nonNullList(installedBundles);
    blacklistedBundles = nonNullList(blacklistedBundles);
    startupFeatures = nonNullList(startupFeatures);
    bootFeatures = nonNullList(bootFeatures);
    installedFeatures = nonNullList(installedFeatures);
    blacklistedFeatures = nonNullList(blacklistedFeatures);
    startupProfiles = nonNullList(startupProfiles);
    bootProfiles = nonNullList(bootProfiles);
    installedProfiles = nonNullList(installedProfiles);
    blacklistedProfiles = nonNullList(blacklistedProfiles);
    blacklistedRepositories = nonNullList(blacklistedRepositories);
    if (!startupProfiles.isEmpty() || !bootProfiles.isEmpty() || !installedProfiles.isEmpty()) {
        if (profilesUri == null) {
            throw new IllegalArgumentException("profilesDirectory must be specified");
        }
    }
    if (featureRepositories != null && !featureRepositories.isEmpty()) {
        getLog().warn("Use of featureRepositories is deprecated, use startupRepositories, bootRepositories or installedRepositories instead");
        startupRepositories.addAll(featureRepositories);
        bootRepositories.addAll(featureRepositories);
        installedRepositories.addAll(featureRepositories);
    }
    StringBuilder remote = new StringBuilder();
    for (Object obj : project.getRemoteProjectRepositories()) {
        if (remote.length() > 0) {
            remote.append(",");
        }
        remote.append(invoke(obj, "getUrl"));
        remote.append("@id=").append(invoke(obj, "getId"));
        if (!((Boolean) invoke(getPolicy(obj, false), "isEnabled"))) {
            remote.append("@noreleases");
        }
        if ((Boolean) invoke(getPolicy(obj, true), "isEnabled")) {
            remote.append("@snapshots");
        }
    }
    getLog().info("Using repositories: " + remote.toString());
    Builder builder = Builder.newInstance();
    builder.offline(mavenSession.isOffline());
    builder.localRepository(localRepo.getBasedir());
    builder.mavenRepositories(remote.toString());
    builder.javase(javase);
    // Set up config and system props
    if (config != null) {
        config.forEach(builder::config);
    }
    if (system != null) {
        system.forEach(builder::system);
    }
    // Set up blacklisted items
    builder.blacklistBundles(blacklistedBundles);
    builder.blacklistFeatures(blacklistedFeatures);
    builder.blacklistProfiles(blacklistedProfiles);
    builder.blacklistRepositories(blacklistedRepositories);
    builder.blacklistPolicy(blacklistPolicy);
    if (propertyFileEdits != null) {
        File file = new File(propertyFileEdits);
        if (file.exists()) {
            KarafPropertyEdits edits;
            try (InputStream editsStream = new FileInputStream(propertyFileEdits)) {
                KarafPropertyInstructionsModelStaxReader kipmsr = new KarafPropertyInstructionsModelStaxReader();
                edits = kipmsr.read(editsStream, true);
            }
            builder.propertyEdits(edits);
        }
    }
    builder.pidsToExtract(pidsToExtract);
    Map<String, String> urls = new HashMap<>();
    List<Artifact> artifacts = new ArrayList<>(project.getAttachedArtifacts());
    artifacts.add(project.getArtifact());
    for (Artifact artifact : artifacts) {
        if (artifact.getFile() != null && artifact.getFile().exists()) {
            String mvnUrl = "mvn:" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/" + artifact.getVersion();
            String type = artifact.getType();
            if ("bundle".equals(type)) {
                type = "jar";
            }
            if (!"jar".equals(type) || artifact.getClassifier() != null) {
                mvnUrl += "/" + type;
                if (artifact.getClassifier() != null) {
                    mvnUrl += "/" + artifact.getClassifier();
                }
            }
            urls.put(mvnUrl, artifact.getFile().toURI().toString());
        }
    }
    if (translatedUrls != null) {
        urls.putAll(translatedUrls);
    }
    builder.translatedUrls(urls);
    // creating system directory
    getLog().info("Creating work directory");
    builder.homeDirectory(workDirectory.toPath());
    IoUtils.deleteRecursive(workDirectory);
    workDirectory.mkdirs();
    List<String> startupKars = new ArrayList<>();
    List<String> bootKars = new ArrayList<>();
    List<String> installedKars = new ArrayList<>();
    // Loading kars and features repositories
    getLog().info("Loading kar and features repositories dependencies");
    for (Artifact artifact : project.getDependencyArtifacts()) {
        Builder.Stage stage;
        switch(artifact.getScope()) {
            case "compile":
                stage = Builder.Stage.Startup;
                break;
            case "runtime":
                stage = Builder.Stage.Boot;
                break;
            case "provided":
                stage = Builder.Stage.Installed;
                break;
            default:
                continue;
        }
        if ("kar".equals(artifact.getType())) {
            String uri = artifactToMvn(artifact);
            switch(stage) {
                case Startup:
                    startupKars.add(uri);
                    break;
                case Boot:
                    bootKars.add(uri);
                    break;
                case Installed:
                    installedKars.add(uri);
                    break;
            }
        } else if ("features".equals(artifact.getClassifier()) || "karaf".equals(artifact.getClassifier())) {
            String uri = artifactToMvn(artifact);
            switch(stage) {
                case Startup:
                    startupRepositories.add(uri);
                    break;
                case Boot:
                    bootRepositories.add(uri);
                    break;
                case Installed:
                    installedRepositories.add(uri);
                    break;
            }
        } else if ("jar".equals(artifact.getType()) || "bundle".equals(artifact.getType())) {
            String uri = artifactToMvn(artifact);
            switch(stage) {
                case Startup:
                    startupBundles.add(uri);
                    break;
                case Boot:
                    bootBundles.add(uri);
                    break;
                case Installed:
                    installedBundles.add(uri);
                    break;
            }
        }
    }
    builder.karafVersion(karafVersion).useReferenceUrls(useReferenceUrls).defaultAddAll(installAllFeaturesByDefault).ignoreDependencyFlag(ignoreDependencyFlag);
    if (profilesUri != null) {
        builder.profilesUris(profilesUri);
    }
    if (libraries != null) {
        builder.libraries(libraries.toArray(new String[libraries.size()]));
    }
    // Startup
    boolean hasFrameworkKar = false;
    for (String kar : startupKars) {
        if (kar.startsWith("mvn:org.apache.karaf.features/framework/") || kar.startsWith("mvn:org.apache.karaf.features/static/")) {
            hasFrameworkKar = true;
            startupKars.remove(kar);
            if (framework == null) {
                framework = kar.startsWith("mvn:org.apache.karaf.features/framework/") ? "framework" : "static-framework";
            }
            builder.kars(Builder.Stage.Startup, false, kar);
            break;
        }
    }
    if (!hasFrameworkKar) {
        Properties versions = new Properties();
        try (InputStream is = getClass().getResourceAsStream("versions.properties")) {
            versions.load(is);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        String realKarafVersion = versions.getProperty("karaf-version");
        String kar;
        switch(framework) {
            case "framework":
                kar = "mvn:org.apache.karaf.features/framework/" + realKarafVersion + "/xml/features";
                break;
            case "framework-logback":
                kar = "mvn:org.apache.karaf.features/framework/" + realKarafVersion + "/xml/features";
                break;
            case "static-framework":
                kar = "mvn:org.apache.karaf.features/static/" + realKarafVersion + "/xml/features";
                break;
            case "static-framework-logback":
                kar = "mvn:org.apache.karaf.features/static/" + realKarafVersion + "/xml/features";
                break;
            default:
                throw new IllegalArgumentException("Unsupported framework: " + framework);
        }
        builder.kars(Builder.Stage.Startup, false, kar);
    }
    if (!startupFeatures.contains(framework)) {
        builder.features(Builder.Stage.Startup, framework);
    }
    builder.defaultStage(Builder.Stage.Startup).kars(toArray(startupKars)).repositories(startupFeatures.isEmpty() && startupProfiles.isEmpty() && installAllFeaturesByDefault, toArray(startupRepositories)).features(toArray(startupFeatures)).bundles(toArray(startupBundles)).profiles(toArray(startupProfiles));
    // Boot
    builder.defaultStage(Builder.Stage.Boot).kars(toArray(bootKars)).repositories(bootFeatures.isEmpty() && bootProfiles.isEmpty() && installAllFeaturesByDefault, toArray(bootRepositories)).features(toArray(bootFeatures)).bundles(toArray(bootBundles)).profiles(toArray(bootProfiles));
    // Installed
    builder.defaultStage(Builder.Stage.Installed).kars(toArray(installedKars)).repositories(installedFeatures.isEmpty() && installedProfiles.isEmpty() && installAllFeaturesByDefault, toArray(installedRepositories)).features(toArray(installedFeatures)).bundles(toArray(installedBundles)).profiles(toArray(installedProfiles));
    // Generate the assembly
    builder.generateAssembly();
    // Include project classes content
    if (includeBuildOutputDirectory)
        IoUtils.copyDirectory(new File(project.getBuild().getOutputDirectory()), workDirectory);
    // Overwrite assembly dir contents
    if (sourceDirectory.exists())
        IoUtils.copyDirectory(sourceDirectory, workDirectory);
    // Chmod the bin/* scripts
    File[] files = new File(workDirectory, "bin").listFiles();
    if (files != null) {
        for (File file : files) {
            if (!file.getName().endsWith(".bat")) {
                try {
                    Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("rwxr-xr-x"));
                } catch (Throwable ignore) {
                // we tried our best, perhaps the OS does not support posix file perms.
                }
            }
        }
    }
}
Also used : KarafPropertyEdits(org.apache.karaf.tools.utils.model.KarafPropertyEdits) HashMap(java.util.HashMap) Builder(org.apache.karaf.profile.assembly.Builder) ArrayList(java.util.ArrayList) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Artifact(org.apache.maven.artifact.Artifact) KarafPropertyInstructionsModelStaxReader(org.apache.karaf.tools.utils.model.io.stax.KarafPropertyInstructionsModelStaxReader) File(java.io.File)

Example 2 with KarafPropertyInstructionsModelStaxReader

use of org.apache.karaf.tools.utils.model.io.stax.KarafPropertyInstructionsModelStaxReader in project karaf by apache.

the class KarafPropertiesEditorTest method onceOver.

@Test
public void onceOver() throws Exception {
    KarafPropertyInstructionsModelStaxReader kipmsr = new KarafPropertyInstructionsModelStaxReader();
    URL editsUrl = Resources.getResource(KarafPropertiesEditorTest.class, "test-edits.xml");
    KarafPropertyEdits edits;
    try (InputStream editsStream = Resources.asByteSource(editsUrl).openStream()) {
        edits = kipmsr.read(editsStream, true);
    }
    Path path = FileSystems.getDefault().getPath("target");
    Path outputEtc = Files.createTempDirectory(path, "test-etc");
    outputEtc.toFile().deleteOnExit();
    KarafPropertiesEditor editor = new KarafPropertiesEditor();
    editor.setInputEtc(new File(ETC_TO_START_WITH)).setOutputEtc(outputEtc.toFile()).setEdits(edits);
    editor.run();
    File resultConfigProps = new File(outputEtc.toFile(), "config.properties");
    Properties properties = new Properties();
    try (InputStream resultInputStream = new FileInputStream(resultConfigProps)) {
        properties.load(resultInputStream);
    }
    assertEquals("equinox", properties.getProperty("karaf.framework"));
    assertEquals("prepended,root,toor", properties.getProperty("karaf.name"));
    resultConfigProps = new File(outputEtc.toFile(), "jre.properties");
    try (InputStream resultInputStream = new FileInputStream(resultConfigProps)) {
        properties.load(resultInputStream);
    }
    assertEquals("This is the cereal: shot from guns", properties.getProperty("test-add-one"));
    assertEquals("This is the gun that shoots cereal", properties.getProperty("test-add-two"));
}
Also used : Path(java.nio.file.Path) KarafPropertyInstructionsModelStaxReader(org.apache.karaf.tools.utils.model.io.stax.KarafPropertyInstructionsModelStaxReader) KarafPropertyEdits(org.apache.karaf.tools.utils.model.KarafPropertyEdits) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Properties (java.util.Properties)2 KarafPropertyEdits (org.apache.karaf.tools.utils.model.KarafPropertyEdits)2 KarafPropertyInstructionsModelStaxReader (org.apache.karaf.tools.utils.model.io.stax.KarafPropertyInstructionsModelStaxReader)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Builder (org.apache.karaf.profile.assembly.Builder)1 Artifact (org.apache.maven.artifact.Artifact)1 Test (org.junit.Test)1