Search in sources :

Example 26 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testCanInvokeMBean2.

public void testCanInvokeMBean2() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanParameterInfo[] sig = new MBeanParameterInfo[] { new MBeanParameterInfo("arg1", "java.lang.String", "") };
    MBeanOperationInfo op = new MBeanOperationInfo("doit", "", sig, "int", MBeanOperationInfo.INFO);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] { op }).anyTimes();
    EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] {}).anyTimes();
    EasyMock.replay(info);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("doit(java.lang.String)[/11/]", "admin");
    configuration.put("doit(java.lang.String)", "admin");
    configuration.put("doit(java.lang.String,java.lang.String)", "admin");
    configuration.put("doit(int)[\"12\"]", "admin");
    configuration.put("doit", "admin");
    configuration.put("do*", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("viewer");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            assertFalse(guard.canInvoke(mbs, on));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 27 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testCanInvokeMBeanGetter2.

public void testCanInvokeMBeanGetter2() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("a1", "boolean", "", true, false, false);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] {}).anyTimes();
    EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr }).anyTimes();
    EasyMock.replay(info);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("get*", "admin");
    configuration.put("is*", "viewer");
    configuration.put("*", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("viewer");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            assertFalse(guard.canInvoke(mbs, on));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 28 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testCanInvokeMBeanSetter2.

public void testCanInvokeMBeanSetter2() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("A2", "java.lang.String", "", true, true, false);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] {}).anyTimes();
    EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr }).anyTimes();
    EasyMock.replay(info);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("get*", "admin");
    configuration.put("setA2", "admin");
    configuration.put("*", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("viewer");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            assertFalse(guard.canInvoke(mbs, on));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 29 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project sling by apache.

the class SlingLogPanel method configureLogger.

/**
     * Configures the logger with the given pid. If the pid is empty a new logger configuration is created.
     *
     * @param pid      configuration pid of the logger
     * @param logLevel the log level to set
     * @param loggers  list of logger categories to set
     * @param logFile  log file (relative path is ok)
     * @param additive logger additivity
     * @throws IOException            when an existing configuration couldn't be updated or a configuration couldn't be created.
     * @throws ConfigurationException when mandatory parameters where not specified
     */
private void configureLogger(final String pid, final String logLevel, final String[] loggers, final String logFile, boolean additive) throws IOException, ConfigurationException {
    // try to get the configadmin service reference
    ServiceReference sr = this.bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
    if (sr != null) {
        try {
            if (logLevel == null) {
                throw new ConfigurationException(LogConfigManager.LOG_LEVEL, "Log level has to be specified.");
            }
            if (loggers == null) {
                throw new ConfigurationException(LogConfigManager.LOG_LOGGERS, "Logger categories have to be specified.");
            }
            if (logFile == null) {
                throw new ConfigurationException(LogConfigManager.LOG_FILE, "LogFile name has to be specified.");
            }
            // try to get the configadmin
            final ConfigurationAdmin configAdmin = (ConfigurationAdmin) this.bundleContext.getService(sr);
            if (configAdmin != null) {
                Configuration config;
                if (pid == null || pid.length() == 0) {
                    config = configAdmin.createFactoryConfiguration(LogConfigManager.FACTORY_PID_CONFIGS);
                } else {
                    config = configAdmin.getConfiguration(pid);
                }
                if (config != null) {
                    Dictionary<String, Object> dict = new Hashtable<String, Object>();
                    dict.put(LogConfigManager.LOG_LEVEL, logLevel.toLowerCase());
                    dict.put(LogConfigManager.LOG_LOGGERS, loggers);
                    dict.put(LogConfigManager.LOG_FILE, logFile);
                    if (additive) {
                        dict.put(LogConfigManager.LOG_ADDITIV, "true");
                    } else {
                        dict.put(LogConfigManager.LOG_ADDITIV, "false");
                    }
                    config.update(dict);
                }
            }
        } finally {
            // release the configadmin reference
            this.bundleContext.ungetService(sr);
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ConfigurationException(org.apache.sling.commons.log.logback.internal.config.ConfigurationException) Hashtable(java.util.Hashtable) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference)

Example 30 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project aries by apache.

the class DSLTest method testConfiguration.

@Test
public void testConfiguration() throws IOException, InterruptedException {
    ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
    ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
    AtomicReference<Dictionary<?, ?>> atomicReference = new AtomicReference<>(null);
    Configuration configuration = null;
    CountDownLatch countDownLatch = new CountDownLatch(1);
    try (OSGiResult<Dictionary<String, ?>> result = configuration("test.configuration").run(bundleContext, x -> {
        atomicReference.set(x);
        countDownLatch.countDown();
    })) {
        assertNull(atomicReference.get());
        configuration = configurationAdmin.getConfiguration("test.configuration");
        configuration.update(new Hashtable<>());
        countDownLatch.await(10, TimeUnit.SECONDS);
        assertNotNull(atomicReference.get());
    } finally {
        bundleContext.ungetService(serviceReference);
        if (configuration != null) {
            configuration.delete();
        }
    }
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Aggregations

ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)211 Configuration (org.osgi.service.cm.Configuration)114 Test (org.junit.Test)65 Hashtable (java.util.Hashtable)57 ServiceReference (org.osgi.framework.ServiceReference)52 IOException (java.io.IOException)44 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 BundleContext (org.osgi.framework.BundleContext)25 Subject (javax.security.auth.Subject)24 Dictionary (java.util.Dictionary)23 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)21 Bundle (org.osgi.framework.Bundle)16 Properties (java.util.Properties)15 File (java.io.File)8 HashSet (java.util.HashSet)7 BundleException (org.osgi.framework.BundleException)7 Method (java.lang.reflect.Method)6 ObjectName (javax.management.ObjectName)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4