Search in sources :

Example 11 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class BaseJDBCLockTest method setUpTestSuite.

@BeforeClass
public static void setUpTestSuite() {
    Properties properties = new Properties();
    properties.put("karaf.bootstrap.log", "target/karaf.log");
    BootstrapLogManager.setProperties(properties);
}
Also used : Properties(org.apache.felix.utils.properties.Properties) BeforeClass(org.junit.BeforeClass)

Example 12 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class SystemServiceImplTest method testSetName.

@Test
public void testSetName() throws URISyntaxException, IOException {
    SystemServiceImpl system = new SystemServiceImpl();
    BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
    URL propUrl = this.getClass().getClassLoader().getResource("etc/system.properties");
    File propfile = new File(propUrl.toURI());
    EasyMock.expect(bundleContext.getProperty("karaf.etc")).andReturn(propfile.getParentFile().getParent() + "/etc");
    EasyMock.replay(bundleContext);
    system.setBundleContext(bundleContext);
    system.setName(NEW_NAME);
    EasyMock.verify(bundleContext);
    Properties props = new Properties(propfile);
    String nameAfter = props.getProperty("karaf.name");
    Assert.assertEquals(NEW_NAME, nameAfter);
}
Also used : Properties(org.apache.felix.utils.properties.Properties) File(java.io.File) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 13 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class SystemServiceImpl method setFramework.

public void setFramework(FrameworkType framework) {
    if (framework == null) {
        return;
    }
    try {
        Properties properties = loadProps();
        properties.put("karaf.framework", framework.name());
        properties.save();
    } catch (IOException e) {
        throw new RuntimeException("Error setting framework: " + e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties)

Example 14 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class Builder method doGenerateAssembly.

private void doGenerateAssembly() throws Exception {
    systemDirectory = homeDirectory.resolve("system");
    etcDirectory = homeDirectory.resolve("etc");
    LOGGER.info("Generating karaf assembly: " + homeDirectory);
    //
    // Create download manager
    //
    Dictionary<String, String> props = new Hashtable<>();
    if (offline) {
        props.put(ORG_OPS4J_PAX_URL_MVN_PID + "offline", "true");
    }
    if (localRepository != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".localRepository", localRepository);
    }
    if (mavenRepositories != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".repositories", mavenRepositories);
    }
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, ORG_OPS4J_PAX_URL_MVN_PID);
    executor = Executors.newScheduledThreadPool(8);
    manager = new CustomDownloadManager(resolver, executor, null, translatedUrls);
    this.resolver = new ResolverImpl(new Slf4jResolverLog(LOGGER));
    //
    // Unzip kars
    //
    LOGGER.info("Unzipping kars");
    Map<String, RepositoryInfo> repositories = new LinkedHashMap<>(this.repositories);
    Downloader downloader = manager.createDownloader();
    for (String kar : kars.keySet()) {
        downloader.download(kar, null);
    }
    downloader.await();
    for (String karUri : kars.keySet()) {
        Kar kar = new Kar(manager.getProviders().get(karUri).getFile().toURI());
        kar.extract(systemDirectory.toFile(), homeDirectory.toFile());
        RepositoryInfo info = kars.get(karUri);
        for (URI repositoryUri : kar.getFeatureRepos()) {
            repositories.put(repositoryUri.toString(), info);
        }
    }
    //
    // Propagate feature installation from repositories
    //
    Map<String, Stage> features = new LinkedHashMap<>(this.features);
    Map<String, Features> karRepositories = loadRepositories(manager, repositories.keySet(), false);
    for (String repo : repositories.keySet()) {
        RepositoryInfo info = repositories.get(repo);
        if (info.addAll) {
            for (Feature feature : karRepositories.get(repo).getFeature()) {
                features.put(feature.getId(), info.stage);
            }
        }
    }
    //
    // Load profiles
    //
    LOGGER.info("Loading profiles");
    allProfiles = new HashMap<>();
    for (String profilesUri : profilesUris) {
        String uri = profilesUri;
        if (uri.startsWith("jar:") && uri.contains("!/")) {
            uri = uri.substring("jar:".length(), uri.indexOf("!/"));
        }
        if (!uri.startsWith("file:")) {
            downloader = manager.createDownloader();
            downloader.download(uri, null);
            downloader.await();
            StreamProvider provider = manager.getProviders().get(uri);
            profilesUri = profilesUri.replace(uri, provider.getFile().toURI().toString());
        }
        URI profileURI = URI.create(profilesUri);
        Path profilePath;
        try {
            profilePath = Paths.get(profileURI);
        } catch (FileSystemNotFoundException e) {
            // file system does not exist, try to create it
            FileSystem fs = FileSystems.newFileSystem(profileURI, new HashMap<>(), Builder.class.getClassLoader());
            profilePath = fs.provider().getPath(profileURI);
        }
        allProfiles.putAll(Profiles.loadProfiles(profilePath));
        // Handle blacklisted profiles
        if (!blacklistedProfiles.isEmpty()) {
            if (blacklistPolicy == BlacklistPolicy.Discard) {
                // Override blacklisted profiles with empty ones
                for (String profile : blacklistedProfiles) {
                    allProfiles.put(profile, ProfileBuilder.Factory.create(profile).getProfile());
                }
            } else {
                // Remove profiles completely
                allProfiles.keySet().removeAll(blacklistedProfiles);
            }
        }
    }
    // Generate profiles
    Profile startupProfile = generateProfile(Stage.Startup, profiles, repositories, features, bundles);
    profiles.put(startupProfile.getId(), Stage.Boot);
    Profile bootProfile = generateProfile(Stage.Boot, profiles, repositories, features, bundles);
    Profile installedProfile = generateProfile(Stage.Installed, profiles, repositories, features, bundles);
    //
    // Compute overall profile
    //
    ProfileBuilder builder = ProfileBuilder.Factory.create(UUID.randomUUID().toString()).setParents(Arrays.asList(startupProfile.getId(), bootProfile.getId(), installedProfile.getId()));
    config.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.CONFIG_PREFIX + k, v));
    system.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.SYSTEM_PREFIX + k, v));
    Profile overallProfile = builder.getProfile();
    Profile overallOverlay = Profiles.getOverlay(overallProfile, allProfiles, environment);
    Profile overallEffective = Profiles.getEffective(overallOverlay, false);
    manager = new CustomDownloadManager(resolver, executor, overallEffective, translatedUrls);
    //        Hashtable<String, String> agentProps = new Hashtable<>(overallEffective.getConfiguration(ORG_OPS4J_PAX_URL_MVN_PID));
    //        final Map<String, String> properties = new HashMap<>();
    //        properties.put("karaf.default.repository", "system");
    //        InterpolationHelper.performSubstitution(agentProps, properties::get, false, false, true);
    //
    // Write config and system properties
    //
    Path configPropertiesPath = etcDirectory.resolve("config.properties");
    Properties configProperties = new Properties(configPropertiesPath.toFile());
    configProperties.putAll(overallEffective.getConfig());
    configProperties.save();
    Path systemPropertiesPath = etcDirectory.resolve("system.properties");
    Properties systemProperties = new Properties(systemPropertiesPath.toFile());
    systemProperties.putAll(overallEffective.getSystem());
    systemProperties.save();
    //
    // Download libraries
    //
    // TODO: handle karaf 2.x and 3.x libraries
    LOGGER.info("Downloading libraries");
    downloader = manager.createDownloader();
    downloadLibraries(downloader, configProperties, overallEffective.getLibraries(), "");
    downloadLibraries(downloader, configProperties, libraries, "");
    downloader.await();
    // Reformat clauses
    reformatClauses(configProperties, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
    reformatClauses(configProperties, Constants.FRAMEWORK_BOOTDELEGATION);
    configProperties.save();
    //
    // Write all configuration files
    //
    LOGGER.info("Writing configurations");
    for (Map.Entry<String, byte[]> config : overallEffective.getFileConfigurations().entrySet()) {
        Path configFile = etcDirectory.resolve(config.getKey());
        LOGGER.info("   adding config file: {}", homeDirectory.relativize(configFile));
        Files.createDirectories(configFile.getParent());
        Files.write(configFile, config.getValue());
    }
    // 'improve' configuration files.
    if (propertyEdits != null) {
        KarafPropertiesEditor editor = new KarafPropertiesEditor();
        editor.setInputEtc(etcDirectory.toFile()).setOutputEtc(etcDirectory.toFile()).setEdits(propertyEdits);
        editor.run();
    }
    //
    if (!overallEffective.getOverrides().isEmpty()) {
        Path overrides = etcDirectory.resolve("override.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        lines.addAll(overallEffective.getOverrides());
        LOGGER.info("Generating {}", homeDirectory.relativize(overrides));
        Files.write(overrides, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    if (!blacklistedFeatures.isEmpty() || !blacklistedBundles.isEmpty()) {
        Path blacklist = etcDirectory.resolve("blacklisted.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        if (!blacklistedFeatures.isEmpty()) {
            lines.add("");
            lines.add("# Features");
            lines.addAll(blacklistedFeatures);
        }
        if (!blacklistedBundles.isEmpty()) {
            lines.add("");
            lines.add("# Bundles");
            lines.addAll(blacklistedBundles);
        }
        LOGGER.info("Generating {}", homeDirectory.relativize(blacklist));
        Files.write(blacklist, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    // Startup stage
    //
    Profile startupEffective = startupStage(startupProfile);
    //
    // Boot stage
    //
    Set<Feature> allBootFeatures = bootStage(bootProfile, startupEffective);
    //
    // Installed stage
    //
    installStage(installedProfile, allBootFeatures);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Downloader(org.apache.karaf.features.internal.download.Downloader) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Properties(org.apache.felix.utils.properties.Properties) URI(java.net.URI) Feature(org.apache.karaf.features.internal.model.Feature) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Profile(org.apache.karaf.profile.Profile) LinkedHashMap(java.util.LinkedHashMap) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) Features(org.apache.karaf.features.internal.model.Features) StreamProvider(org.apache.karaf.features.internal.download.StreamProvider) Hashtable(java.util.Hashtable) KarafPropertiesEditor(org.apache.karaf.tools.utils.KarafPropertiesEditor) Kar(org.apache.karaf.kar.internal.Kar) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 15 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class Builder method startupStage.

private Profile startupStage(Profile startupProfile) throws Exception {
    LOGGER.info("Startup stage");
    //
    // Compute startup
    //
    Profile startupOverlay = Profiles.getOverlay(startupProfile, allProfiles, environment);
    Profile startupEffective = Profiles.getEffective(startupOverlay, false);
    // Load startup repositories
    LOGGER.info("   Loading repositories");
    Map<String, Features> startupRepositories = loadRepositories(manager, startupEffective.getRepositories(), false);
    //
    // Resolve
    //
    LOGGER.info("   Resolving features");
    Map<String, Integer> bundles = resolve(manager, resolver, startupRepositories.values(), startupEffective.getFeatures(), startupEffective.getBundles(), startupEffective.getOverrides(), startupEffective.getOptionals());
    //
    // Generate startup.properties
    //
    Properties startup = new Properties();
    startup.setHeader(Collections.singletonList("# Bundles to be started on startup, with startlevel"));
    Map<Integer, Set<String>> invertedStartupBundles = MapUtils.invert(bundles);
    for (Map.Entry<Integer, Set<String>> entry : new TreeMap<>(invertedStartupBundles).entrySet()) {
        String startLevel = Integer.toString(entry.getKey());
        for (String location : new TreeSet<>(entry.getValue())) {
            if (useReferenceUrls) {
                if (location.startsWith("mvn:")) {
                    location = "file:" + Parser.pathFromMaven(location);
                }
                if (location.startsWith("file:")) {
                    location = "reference:" + location;
                }
            }
            if (location.startsWith("file:") && karafVersion == KarafVersion.v24) {
                location = location.substring("file:".length());
            }
            startup.put(location, startLevel);
        }
    }
    Path startupProperties = etcDirectory.resolve("startup.properties");
    startup.save(startupProperties.toFile());
    return startupEffective;
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Properties(org.apache.felix.utils.properties.Properties) Profile(org.apache.karaf.profile.Profile) TreeSet(java.util.TreeSet) Features(org.apache.karaf.features.internal.model.Features) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Aggregations

Properties (org.apache.felix.utils.properties.Properties)64 File (java.io.File)26 Test (org.junit.Test)22 IOException (java.io.IOException)21 Subject (javax.security.auth.Subject)21 NamePasswordCallbackHandler (org.apache.karaf.jaas.modules.NamePasswordCallbackHandler)21 RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)16 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)16 Principal (java.security.Principal)12 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 GroupPrincipal (org.apache.karaf.jaas.boot.principal.GroupPrincipal)6 Map (java.util.Map)5 AbstractKerberosITest (org.apache.directory.server.kerberos.kdc.AbstractKerberosITest)5 FileInputStream (java.io.FileInputStream)4 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 LinkedHashMap (java.util.LinkedHashMap)4 TreeMap (java.util.TreeMap)4 FailedLoginException (javax.security.auth.login.FailedLoginException)4