Skip navigation links

Package com.prosyst.mprm.backend.database.system

This package provides access to the System Database(in mPRM terms).

See: Description

Package com.prosyst.mprm.backend.database.system Description

This package provides access to the System Database(in mPRM terms). The System Database database contains the most sensitive information of a mPRM system - The Host`s Configuration, The Database Server & Service`s Configuration, The Bundle`s Configuration.
The class providing this functionality is the SystemDatabase interface. The SystemDatabase is not accessible as OSGi service. To get an instance of it use SystemDatabaseFactory It is available as service on any Backend Server Host.

Example of retreiving SystemDatabase isntance:

 import com.prosyst.mprm.admin.system.DatabaseServerConfiguration;

 import com.prosyst.mprm.backend.database.system.*;

 import java.io.InputStream;
 import java.io.FileInputStream;

 import java.util.Dictionary;
 import java.util.Hashtable;

 import org.osgi.framework.*;

 public class SystemDBProvider implements ServiceListener {

  private static final String FACTORY = SystemDatabaseFactory.class.getName();
  private Dictionary connectProperties;
  private File driver;

  private ServiceReference factoryRef;
  private SystemDatabaseFactory factory;
  private SystemDatabase database;
  private String systemId;
  private BundleContext bCtx;

  //pass all the needed data here
  //connection properties can be as follows(for McKoi database server running as external application)
  //Dictionary connectProperties = new Hashtable();
  //connectProperties.put(DatabaseServerConfiguration.URL, "jdbc:mckoi://localhost");
  //connectProperties.put(DatabaseServerConfiguration.USER, "admin");
  //connectProperties.put(DatabaseServerConfiguration.PASSWORD, "admin");
  //connectProperties.put(DatabaseServerConfiguration.MAX_CONNECTIONS, String.valueOf(3));
  //connectProperties.put(DatabaseServerConfiguration.MAX_STATEMENTS, String.valueOf(10));
  //connectProperties.put(DatabaseServerConfiguration.DRIVER, "com.mckoi.JDBCDriver");
  //connectProperties.put(DatabaseServerConfiguration.DRIVER_LIB_NAME, "mkjdbc.jar");
  public void serve(BundleContext $bCtx, Dictionary $connectProperties, File $driver, String $systemId) {
   bCtx = $bCtx;
   connectProperties = $connectProperties;
   driver = $driver;
   systemId = $systemId;

   try {
    bCtx.addServiceListener(this, '(' + Constants.OBJECTCLASS + '=' + FACTORY + ')');
   } catch (Exception exc) {
    exc.printStackTrace();
   }

   initRefs(bCtx.getServiceReference(FACTORY));
  }

  public void release() {
   try {
    bCtx.removeServiceListener(this);

    releaseRefs();
   } catch (Exception exc) {
    exc.printStackTrace();
   }
  }

  public void serviceChanged(ServiceEvent event) {
   switch (event.getType()) {
    case (ServiceEvent.UNREGISTERING) : {
     if (event.getServiceReference().equals(factoryRef)) {
      releaseRefs();
      initRefs();
     }
     break;
    }
    case (ServiceEvent.REGISTERED) : {
     if (factory == null) {
      initRefs(event.getServiceReference());
     }
     break;
    }
   }
  }

  public synchronized SystemDatabase getService() throws Exception {
   if (database == null) {
    if (factory == null) {
     throw new Exception("SystemDatabaseFactory service not present.");
    }
    InputStream driverStream = new FileInputStream(driiver);
    connectProperties.put(DatabaseServerConfiguration.DRIVER_LIB, driverStream);
    try {
     database = factory.getInstance(systemId, connectProperties);
    } finally {
     if (driverStream != null) {
      try {
       driverStream.close();
      } catch (Exception exc) {
      }
     }
     connectProperties.remove(DatabaseServerConfiguration.DRIVER_LIB);
    }
   }
   return database;
  }

  private void initRefs() {
   if (bCtx.getBundle(0).getState() != Bundle.STOPPING) {
    initRefs(bCtx.getServiceReference(FACTORY));
   }
  }

  private void initRefs(ServiceReference sRef) {
   if (sRef == null) {
    return;
   }

   Object service = bCtx.getService(sRef);
   if (service == null) {
    return;
   }

   factoryRef = sRef;

   factory = (SystemDatabaseFactory) service;
  }

  private void releaseRefs() {
   if (factoryRef != null) {
    if (database != null) {
     try {
      factory.close(database);
     } catch (Exception exc) {
      exc.printStackTrace();
     }
    }
    database = null;
    factory = null;

    bCtx.ungetService(factoryRef);

    factoryRef = null;
   }
  }
 }

Skip navigation links

Copyright © 2019 Bosch Software Innovations GmbH. All Rights Reserved.