/**
 * 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.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);
        OdbConfiguration.setNumberOfRetryToOpenFile(3);
        OdbConfiguration.setRetryTimeout(1000);
    }

    @Override
    public synchronized PersistenceService getPersistenceForBundle(Bundle bundle) {
        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 String getFileName(Bundle bundle) {
        return bundle.getSymbolicName().hashCode() + "_" + bundle.getVersion().hashCode() + ".data";
    }

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

}

@@ -678,16 +678,37 @@
 ernal;%0A%0A
+import java.io.File;%0A
 import j
@@ -1454,32 +1454,63 @@
 undle bundle) %7B%0A
+        checkRootDirCreated();%0A
         String d
@@ -1967,24 +1967,213 @@
 ice;%0A    %7D%0A%0A
+    private void checkRootDirCreated() %7B%0A        File rootDir = new File(persistenceRootDir);%0A        if (rootDir.exists()) %7B%0A            return;%0A        %7D%0A        rootDir.mkdirs();%0A    %7D%0A%0A
     private 

Create persistence root dir if it is not present to prevent neodatis from writing to sysout.