Search in sources :

Example 6 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class ConfigurationManager method getPersistenceManagers.

// ---------- internal -----------------------------------------------------
private CachingPersistenceManagerProxy[] getPersistenceManagers() {
    int currentPmtCount = persistenceManagerTracker.getTrackingCount();
    if (persistenceManagers == null || currentPmtCount > pmtCount) {
        List pmList = new ArrayList();
        CachingPersistenceManagerProxy[] pm;
        ServiceReference<?>[] refs = persistenceManagerTracker.getServiceReferences();
        if (refs == null || refs.length == 0) {
            pm = new CachingPersistenceManagerProxy[0];
        } else {
            // sort the references according to the cmRanking property
            if (refs.length > 1) {
                Arrays.sort(refs, RankingComparator.SRV_RANKING);
            }
            // create the service array from the sorted set of referenecs
            for (int i = 0; i < refs.length; i++) {
                Object service = persistenceManagerTracker.getService(refs[i]);
                if (service != null) {
                    CachingPersistenceManagerProxy proxy = getProxyForPersistenceManager((PersistenceManager) service);
                    if (proxy == null) {
                        proxy = new CachingPersistenceManagerProxy((PersistenceManager) service);
                    }
                    pmList.add(proxy);
                }
            }
            pm = (CachingPersistenceManagerProxy[]) pmList.toArray(new CachingPersistenceManagerProxy[pmList.size()]);
        }
        pmtCount = currentPmtCount;
        persistenceManagers = pm;
    }
    return persistenceManagers;
}
Also used : PersistenceManager(org.apache.felix.cm.PersistenceManager) FilePersistenceManager(org.apache.felix.cm.file.FilePersistenceManager) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ServiceReference(org.osgi.framework.ServiceReference)

Example 7 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class CachingPersistenceManagerProxyTest method test_caching_is_avoided.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_caching_is_avoided() throws Exception {
    String pid = "testDefaultPersistenceManager";
    SimpleFilter filter = SimpleFilter.parse("(&(service.pid=" + pid + ")(property1=value1))");
    PersistenceManager pm = new MockNotCachablePersistenceManager();
    CachingPersistenceManagerProxy cpm = new CachingPersistenceManagerProxy(pm);
    Dictionary dictionary = new Hashtable();
    dictionary.put("property1", "value1");
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    Enumeration dictionaries = cpm.getDictionaries(filter);
    List list = Collections.list(dictionaries);
    assertEquals(1, list.size());
    dictionary = new Hashtable();
    dictionary.put("property1", "value2");
    pid = "testDefaultPersistenceManager";
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    dictionaries = cpm.getDictionaries(filter);
    list = Collections.list(dictionaries);
    assertEquals(1, list.size());
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) Hashtable(java.util.Hashtable) List(java.util.List) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager)

Example 8 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class CachingPersistenceManagerProxyTest method test_caching_is_applied.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_caching_is_applied() throws Exception {
    String pid = "testDefaultPersistenceManager";
    SimpleFilter filter = SimpleFilter.parse("(&(service.pid=" + pid + ")(property1=value1))");
    PersistenceManager pm = new MockPersistenceManager();
    CachingPersistenceManagerProxy cpm = new CachingPersistenceManagerProxy(pm);
    Dictionary dictionary = new Hashtable();
    dictionary.put("property1", "value1");
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    Enumeration dictionaries = cpm.getDictionaries(filter);
    List list = Collections.list(dictionaries);
    assertEquals(1, list.size());
    dictionary = new Hashtable();
    dictionary.put("property1", "value2");
    pid = "testDefaultPersistenceManager";
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    dictionaries = cpm.getDictionaries(filter);
    list = Collections.list(dictionaries);
    assertEquals(1, list.size());
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) Hashtable(java.util.Hashtable) List(java.util.List) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager)

Example 9 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class Activator method start.

@Override
public void start(final BundleContext bundleContext) throws BundleException {
    // setup log
    Log.logger.start(bundleContext);
    // register default file persistence manager
    final PersistenceManager defaultPM = this.registerFilePersistenceManager(bundleContext);
    if (defaultPM == null) {
        throw new BundleException("Unable to register default persistence manager.");
    }
    String configuredPM = bundleContext.getProperty(CM_CONFIG_PM);
    if (configuredPM != null && configuredPM.isEmpty()) {
        configuredPM = null;
    }
    try {
        this.tracker = new PersistenceManagerTracker(bundleContext, defaultPM, configuredPM);
    } catch (InvalidSyntaxException iae) {
        Log.logger.log(LogService.LOG_ERROR, "Cannot create the persistence manager tracker", iae);
        throw new BundleException(iae.getMessage(), iae);
    }
}
Also used : PersistenceManagerTracker(org.apache.felix.cm.impl.persistence.PersistenceManagerTracker) PersistenceManager(org.apache.felix.cm.PersistenceManager) FilePersistenceManager(org.apache.felix.cm.file.FilePersistenceManager) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleException(org.osgi.framework.BundleException)

Example 10 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class Activator method registerFilePersistenceManager.

private PersistenceManager registerFilePersistenceManager(final BundleContext bundleContext) {
    try {
        final FilePersistenceManager fpm = new FilePersistenceManager(bundleContext, bundleContext.getProperty(CM_CONFIG_DIR));
        final Dictionary<String, Object> props = new Hashtable<>();
        props.put(Constants.SERVICE_DESCRIPTION, "Platform Filesystem Persistence Manager");
        props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
        props.put(Constants.SERVICE_RANKING, new Integer(Integer.MIN_VALUE));
        props.put(PersistenceManager.PROPERTY_NAME, FilePersistenceManager.DEFAULT_PERSISTENCE_MANAGER_NAME);
        filepmRegistration = bundleContext.registerService(PersistenceManager.class, fpm, props);
        return fpm;
    } catch (final IllegalArgumentException iae) {
        Log.logger.log(LogService.LOG_ERROR, "Cannot create the FilePersistenceManager", iae);
    }
    return null;
}
Also used : FilePersistenceManager(org.apache.felix.cm.file.FilePersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) FilePersistenceManager(org.apache.felix.cm.file.FilePersistenceManager) Hashtable(java.util.Hashtable)

Aggregations

PersistenceManager (org.apache.felix.cm.PersistenceManager)15 Hashtable (java.util.Hashtable)8 MockPersistenceManager (org.apache.felix.cm.MockPersistenceManager)7 MockNotCachablePersistenceManager (org.apache.felix.cm.MockNotCachablePersistenceManager)6 Dictionary (java.util.Dictionary)5 List (java.util.List)3 FilePersistenceManager (org.apache.felix.cm.file.FilePersistenceManager)3 CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 Enumeration (java.util.Enumeration)2 SimpleFilter (org.apache.felix.cm.impl.SimpleFilter)2 FabricException (io.fabric8.api.FabricException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 MockBundleContext (org.apache.felix.cm.MockBundleContext)1 NotCachablePersistenceManager (org.apache.felix.cm.NotCachablePersistenceManager)1 EncryptingPersistenceManager (org.apache.felix.cm.file.EncryptingPersistenceManager)1 PersistenceManagerProxy (org.apache.felix.cm.impl.persistence.PersistenceManagerProxy)1