Previous Topic

Next Topic

Book Contents

Book Index

Flash Manager service

The FlashManager service is available only if the mbs.storage.ramroot system property is set.

The interface of the FlashManager service is com.prosyst.mbs.framework.flash.FlashManager.

To be notified that the flush operation on a data file has been successful, implement the com.prosyst.mbs.framework.framework.flash.FlashListener interface and provide it as a parameter to the flushFile method.

Registering bundle

The service is registered by System bundle.

Example

The following listing contains an example, which writes a data file - test.txt, into the RAM storage. Then writes some data in the file, flushes it. When the file is flushed successfully, the example prints the resulting data file to the system output.

Using the FlashManager service:

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.prosyst.mbs.framework.flash.FlashManager;
import com.prosyst.mbs.framework.flash.FileListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;

public class MyFlusher implements BundleActivator, FileListener {
        
  private ServiceReference refFlashManager;
  private FlashManager flashManager;
  final static String SERVICE_FLASHMANAGER = FlashManager.class.getName();
  private BundleContext bc;
  private File flashDataFile;
  private File ramDataFile;

  public void start(BundleContext bc) throws Exception {
    this.bc = bc;
    try {
      refFlashManager = bc.getServiceReference(SERVICE_FLASHMANAGER);
      if (refFlashManager != null) {
        flashManager = (FlashManager)bc.getService(refFlashManager);
        flashDataFile = bc.getDataFile("test.txt");
        ramDataFile = flashManager.getFile(flashDataFile);
        if(ramDataFile != null) {
          changeFile();
          flashManager.flushFile(ramDataFile, this);
        }
      }
    } catch (Exception exc) {
      dump("[MyFlusher]Error in start method: " + exc.getMessage(), exc);
    }
  }

  public void stop(BundleContext bc) throws Exception {
    if (refFlashManager != null) {
      bc.ungetService(refFlashManager);
    }
  }

  public static void dump(String str, Throwable t) {
    if (t != null) {
      t.printStackTrace();
    }
  }

  public void flushed(File file) {
    System.out.println("[MyFlusher]File " + file.getPath() + " flushed.");
    readDataFile();
  }

  private void changeFile(){
    try {
      FileOutputStream ramOut = new FileOutputStream(ramDataFile);
      ramOut.write((new String("[MyFlusher]Data changed on RAM")).getBytes());
      ramOut.close();
    } catch (Exception exc) {
      dump("[MyFlusher]Error in modifying file on RAM: " + exc.getMessage(), exc);
    }
  }
        
  private void readDataFile() {
    try {
      FileInputStream flashIn = new FileInputStream(flashDataFile);
      byte[] buff = new byte[flashIn.available()];
      while(flashIn.read(buff) > 0){
        System.out.println("[MyFlusher]Data flushed : " + new String(buff));
      }
    } catch (Exception exc) {
      dump("[MyFlusher]Error in flushing file: " + exc.getMessage(), exc);
    }
  }
}