/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.core.persistence.internal;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.neodatis.odb.OdbConfiguration;
import org.openengsb.core.persistence.PersistenceManager;
import org.openengsb.core.persistence.PersistenceService;
import org.osgi.framework.Bundle;

public class NeodatisPersistenceManager implements PersistenceManager {

    private String persistenceRootDir;

    private Map<String, PersistenceService> persistenceServices = new HashMap<String, PersistenceService>();

    public NeodatisPersistenceManager() {
        OdbConfiguration.useMultiThread(true);
    }

    @Override
    public synchronized PersistenceService getPersistenceForBundle(Bundle bundle) {
        checkRootDirCreated();
        String dbFile = persistenceRootDir + "/" + getFileName(bundle);
        if (persistenceServices.containsKey(dbFile)) {
            return persistenceServices.get(dbFile);
        }
        CustomClassLoader classLoader = new CustomClassLoader(getClass().getClassLoader(), bundle);
        PersistenceService bundleService = new NeodatisPersistenceService(dbFile, classLoader);
        persistenceServices.put(dbFile, bundleService);
        return bundleService;
    }

    private void checkRootDirCreated() {
        File rootDir = new File(persistenceRootDir);
        if (rootDir.exists()) {
            return;
        }
        rootDir.mkdirs();
    }

    private String getFileName(Bundle bundle) {
        return bundle.getSymbolicName().hashCode() + "_" + bundle.getVersion().hashCode() + ".data";
    }

    public void setPersistenceRootDir(String persistenceRootDir) {
        this.persistenceRootDir = persistenceRootDir;
    }

}

@@ -748,16 +748,101 @@
 l.Map;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -875,16 +875,16 @@
 ration;%0A
-
 import o
@@ -1099,16 +1099,92 @@
 ager %7B%0A%0A
+    private Log log = LogFactory.getLog(NeodatisPersistenceManager.class);%0A%0A
     priv
@@ -1578,34 +1578,38 @@
 e = 
-persistenceRootDir + %22/%22 +
+new File(getAbsoluteRootDir(),
  get
@@ -1620,24 +1620,35 @@
 Name(bundle)
+).getPath()
 ;%0A        if
@@ -2111,35 +2111,26 @@
 r = 
-new File(persistenc
+getAbsolut
 eRootDir
 );%0A 
@@ -2125,16 +2125,17 @@
 eRootDir
+(
 );%0A     
@@ -2183,32 +2183,32 @@
 turn;%0A        %7D%0A
-
         rootDir.
@@ -2216,32 +2216,241 @@
 kdirs();%0A    %7D%0A%0A
+    private File getAbsoluteRootDir() %7B%0A        String karafData = System.getProperty(%22karaf.data%22);%0A        log.info(%22karafData: %22 + karafData);%0A        return new File(karafData, persistenceRootDir);%0A    %7D%0A%0A
     private Stri

make sure persistence uses karaf.data