Package org.objectweb.speedo.j2eedo.test

Source Code of org.objectweb.speedo.j2eedo.test.MainLauncher

/*
* Speedo: an implementation of JDO compliant personality on top of JORM
* generic I/O sub-system. Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Release: 1.0
*
* Created on Apr 19, 2004 @author franck.milleville@cgey.com
*/
package org.objectweb.speedo.j2eedo.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.jdo.JDOException;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.j2eedo.bo.DatabaseImpl;
import org.objectweb.speedo.j2eedo.common.PMHolder;
import org.objectweb.util.monolog.Monolog;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.LoggerFactory;

/**
* This class is used for basic test implementation in order to
* highlight some errors or behaviours. The developper needs to create a new class extended of
* this class and writes its own test method doTest()
*
* @author franck.milleville@cgey.com
*/
public class MainLauncher {
  static Logger logger = null;
  static {
    LoggerFactory lf = Monolog.initialize();
    logger = lf.getLogger(MainLauncher.class.getName());
  }
 
  final static Properties p = new Properties();
  public void initPMF() throws JDOException, Exception {
    //Load the properties file from the classpath
    String speedoProperties = "speedo-jdo.properties";
    InputStream is =
      DatabaseImpl.class.getClassLoader().getResourceAsStream(
          speedoProperties);
    if (is == null) {
      System.err.println(
          "[ERROR] No '"
          + speedoProperties
          + "' property file availlable in the classpath (classloader="
          + DatabaseImpl.class.getClassLoader());
      System.exit(2);
    }
    try {
      p.load(is);
    } catch (IOException e) {
      System.err.println(
          "[ERROR] Impossible to load the '"
          + speedoProperties
          + "' property file from the classpath: "
          + e.getMessage());
      System.exit(2);
    }
    //Specify to speedo to create the mapping structure if it does not
    // already exist
    p.setProperty(
        SpeedoProperties.MAPPING_STRUCTURE,
        SpeedoProperties.MAPPING_STRUCTURE_CIR);
    boolean useDriverDirectly = false;
    String s =
      System.getProperty(
          SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
    if (s != null) {
      p.setProperty(
          SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME,
          s);
      useDriverDirectly = true;
    }
    s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
    if (s != null) {
      p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, s);
      useDriverDirectly = true;
    }
    s = System.getProperty(
          SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME);
    if (s != null) {
      p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, s);
      useDriverDirectly = true;
    }
    s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD);
    if (s != null) {
      p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, s);
      useDriverDirectly = true;
    }
    s = System.getProperty(SpeedoProperties.MAPPER_NAME);
    if (s != null) {
      p.setProperty(SpeedoProperties.MAPPER_NAME, s);
    }

    if (useDriverDirectly) {
      //In case of the user specifies driver properties, the CF name is
      // removed
      p.remove(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME);
    }

    PersistenceManagerFactory pmf =
      JDOHelper.getPersistenceManagerFactory(p);
    PMHolder pmHolder = new PMHolder(pmf);
    DatabaseImpl.initTestData(pmHolder);
  }

  /**
   * This method connects to the database using the default speedo properties
   * file and calls the test method doTest
   *
   * @param args not used
   * @throws JDOException
   * @throws Exception
   * @see #doTest()
   */
  public static void main(String[] args) throws JDOException, Exception {
    MainLauncher ml = new MainLauncher();
    ml.initPMF();
    ml.doTest();
  }

  /**
   * This method is the basic test sample. It describes how to get the
   * persistence manager factory.
   * <p>
   * This method must be rewritten by the include your own tests as shown by the
   * class {@link ShowConcurrencyErrors ShowConcurrencyErrors}
   */
  public void doTest() throws JDOException, Exception {
    // get the persistence manager factory
    PersistenceManagerFactory pmf =
      JDOHelper.getPersistenceManagerFactory(p);
    logger.log(BasicLevel.INFO, "Show connection properties:\n\t" + pmf.getProperties());
  }
}
TOP

Related Classes of org.objectweb.speedo.j2eedo.test.MainLauncher

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.