protected void registerDatasource() {
   try {
     prepareJndi();
     InitialContext ic = new InitialContext();
     // Construct BasicDataSource reference
     Reference ref =
         new Reference(
             "javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
     ref.add(new StringRefAddr("driverClassName", config.getDriver()));
     ref.add(new StringRefAddr("url", config.getUrl()));
     ref.add(new StringRefAddr("username", config.getUsername()));
     ref.add(new StringRefAddr("password", config.getPassword()));
     ref.add(new StringRefAddr("maxActive", "4"));
     ref.add(new StringRefAddr("maxWait", "5000"));
     ref.add(new StringRefAddr("removeAbandoned", "true"));
     ref.add(new StringRefAddr("removeAbandonedTimeout", "5000"));
     rebind(ic, config.getJndiName(), ref);
     ic.rebind(config.getJndiName(), ref);
     rebind(ic, JNDINames.DATABASE_DATASOURCE, ref);
     ic.rebind(JNDINames.DATABASE_DATASOURCE, ref);
     rebind(ic, JNDINames.ADMIN_DATASOURCE, ref);
     ic.rebind(JNDINames.ADMIN_DATASOURCE, ref);
     registerMockJMS(ic);
   } catch (NamingException nex) {
     logger.error(nex);
   } catch (IOException ex) {
     logger.error(ex);
   }
 }
Пример #2
0
  private void registerJNDI(DataSourceMetaInfo dsmInfo, Object dsObject)
      throws DataSourceException {
    try {
      PrivilegedCarbonContext.startTenantFlow();
      PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(this.getTenantId());
      JNDIConfig jndiConfig = dsmInfo.getJndiConfig();
      if (jndiConfig == null) {
        return;
      }
      InitialContext context;
      try {
        context = new InitialContext(jndiConfig.extractHashtableEnv());
      } catch (NamingException e) {
        throw new DataSourceException("Error creating JNDI initial context: " + e.getMessage(), e);
      }
      this.checkAndCreateJNDISubContexts(context, jndiConfig.getName());

      try {
        context.rebind(jndiConfig.getName(), dsObject);
      } catch (NamingException e) {
        throw new DataSourceException(
            "Error in binding to JNDI with name '" + jndiConfig.getName() + "' - " + e.getMessage(),
            e);
      }
    } finally {
      PrivilegedCarbonContext.endTenantFlow();
    }
  }
Пример #3
0
 public void rebind() {
   try {
     InitialContext ic = new InitialContext();
     ic.rebind(JNDI_NAME, "Test2");
   } catch (NamingException e) {
     throw new RuntimeException(e);
   }
 }
Пример #4
0
 public void setConfiguration(Configuration cfg) throws ConfigurationException {
   this.cfg = cfg;
   try {
     InitialContext ctx = new InitialContext();
     ctx.rebind(cfg.get("name"), stub);
   } catch (NamingException e) {
     throw new ConfigurationException(e);
   }
 }
Пример #5
0
  @Before
  public void initJNDI() throws Exception {
    System.out.println("init [TestFlow]");
    this.setUp();
    // Create initial context
    System.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    final InitialContext ic = new InitialContext();

    try {
      ic.destroySubcontext("java:");
    } catch (final Exception e) {
    }

    ic.createSubcontext("java:");
    ic.createSubcontext("java:/comp");
    ic.createSubcontext("java:/comp/env");
    ic.createSubcontext("java:/comp/env/jdbc");
    ic.createSubcontext("java:/comp/env/cfg");
    ic.createSubcontext("java:/comp/env/cfg/Source");
    ic.createSubcontext("java:/comp/env/cfg/ConnectionParameters");

    final ConnectionParameters cp = new ConnectionParameters();
    ic.bind("java:/comp/env/cfg/ConnectionParameters/attributes", cp);
    ic.bind("java:/comp/env/cfg/ConnectionParameters/authentication", cp);

    ic.bind("java:/comp/env/cfg/Source/attributes", "FAKE");
    ic.bind("java:/comp/env/cfg/Source/authentication", "FAKE");
    ic.bind("java:/comp/env/cfg/bindQuery", "FAKE");
    ic.bind("java:/comp/env/cfg/restAuthPassword", "");
    ic.bind("java:/comp/env/cfg/restAuthUser", "flow");
    ic.bind("java:/comp/env/cfg/issuanceServiceURL", getBaseURI() + "issuance/");
    ic.bind("java:/comp/env/cfg/userServiceURL", getBaseURI() + "user/");
    ic.bind("java:/comp/env/cfg/verificationServiceURL", getBaseURI() + "verification/");
    ic.bind("java:/comp/env/cfg/verifierIdentity", "unknown");

    final SQLiteDataSource ds = new SQLiteDataSource();

    storageFile = File.createTempFile("test", "sql");

    ds.setUrl("jdbc:sqlite:" + storageFile.getPath());
    System.out.println(ds.getUrl());
    ic.rebind("java:/comp/env/jdbc/" + dbName, ds);
    ic.bind("java:/comp/env/cfg/useDbLocking", new Boolean(true));

    ic.close();

    RESTHelper.postRequest(issuanceServiceURL + "reset");
    RESTHelper.postRequest(verificationServiceURL + "reset");
    RESTHelper.postRequest(userServiceURL + "reset");

    // System.exit(1);
  }
Пример #6
0
 static void rebindDataSource(
     ServletContext servletContext,
     String jndiName,
     DataSource dataSource,
     DataSource dataSourceProxy)
     throws Throwable {
   final Object lock = changeContextWritable(servletContext, null);
   final InitialContext initialContext = new InitialContext();
   initialContext.rebind(jndiName, dataSourceProxy);
   JNDI_DATASOURCES_BACKUP.put(jndiName, dataSource);
   changeContextWritable(servletContext, lock);
   initialContext.close();
 }
  @Resource
  public void setDataSource(DataSource datasource) {
    this.datasource = datasource;
    try {
      prepareJndi();
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
      InitialContext ic = new InitialContext(env);
      Properties properties = new Properties();
      properties.load(PathTestUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
      // Construct BasicDataSource reference
      Reference ref =
          new Reference(
              "javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
      ref.add(
          new StringRefAddr(
              "driverClassName",
              properties.getProperty("driverClassName", "org.postgresql.Driver")));
      ref.add(
          new StringRefAddr(
              "url", properties.getProperty("url", "jdbc:postgresql://localhost:5432/postgres")));
      ref.add(new StringRefAddr("username", properties.getProperty("username", "postgres")));
      ref.add(new StringRefAddr("password", properties.getProperty("password", "postgres")));
      ref.add(new StringRefAddr("maxActive", "4"));
      ref.add(new StringRefAddr("maxWait", "5000"));
      ref.add(new StringRefAddr("removeAbandoned", "true"));
      ref.add(new StringRefAddr("removeAbandonedTimeout", "5000"));

      rebind(ic, JNDINames.DATABASE_DATASOURCE, ref);
      ic.rebind(JNDINames.DATABASE_DATASOURCE, ref);
      rebind(ic, JNDINames.ADMIN_DATASOURCE, ref);
      ic.rebind(JNDINames.ADMIN_DATASOURCE, ref);
    } catch (NamingException nex) {
      nex.printStackTrace();
    } catch (IOException nex) {
      nex.printStackTrace();
    }
  }
Пример #8
0
 static void rebindInitialDataSources(ServletContext servletContext) throws Throwable {
   try {
     final InitialContext initialContext = new InitialContext();
     for (final Map.Entry<String, DataSource> entry : JNDI_DATASOURCES_BACKUP.entrySet()) {
       final String jndiName = entry.getKey();
       final DataSource dataSource = entry.getValue();
       final Object lock = changeContextWritable(servletContext, null);
       initialContext.rebind(jndiName, dataSource);
       changeContextWritable(servletContext, lock);
     }
     initialContext.close();
   } finally {
     JNDI_DATASOURCES_BACKUP.clear();
   }
 }
Пример #9
0
  @Test
  public void svmUTTest() throws NamingException {
    InitialContext context = new InitialContext(null);
    // ensure the transaction manager is available via JNDI
    JNDIManager.bindJTATransactionManagerImplementation(context);

    ServerVMClientUserTransaction ut = ServerVMClientUserTransaction.getSingleton();

    // validate that ut can be bound to a JNDI context
    context.rebind("ut", ut);

    // validate that the instance that was bound is the same as the ServerVMClientUserTransaction
    // singleton
    Object boundUT = context.lookup("ut");

    assertNotNull(boundUT);
    assertTrue(boundUT instanceof ServerVMClientUserTransaction);
    assertEquals(ut, boundUT);
  }
Пример #10
0
  private void rebind(String unbindName) throws NamingException {
    InitialContext ictx = new InitialContext();
    if (unbindName != null) ictx.unbind(unbindName);

    if (jndiName != null) {
      // Thanks to David D. Kilzer for this code to auto-create
      // subcontext paths!
      Name name = ictx.getNameParser(jndiName).parse(jndiName);
      Context ctx = ictx;
      for (int i = 0, max = name.size() - 1; i < max; i++) {
        try {
          ctx = ctx.createSubcontext(name.get(i));
        } catch (NameAlreadyBoundException ignore) {
          ctx = (Context) ctx.lookup(name.get(i));
        }
      }

      ictx.rebind(jndiName, combods);
    }
  }
 public void configureJNDIDatasource() throws IOException, NamingException {
   prepareJndi();
   Properties env = new Properties();
   env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
   InitialContext ic = new InitialContext(env);
   Properties props = new Properties();
   props.load(SilverpeasJndiCase.class.getClassLoader().getResourceAsStream("jdbc.properties"));
   // Construct BasicDataSource reference
   Reference ref =
       new Reference(
           "javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
   ref.add(new StringRefAddr("driverClassName", props.getProperty("driverClassName")));
   ref.add(new StringRefAddr("url", props.getProperty("url")));
   ref.add(new StringRefAddr("username", props.getProperty("username")));
   ref.add(new StringRefAddr("password", props.getProperty("password")));
   ref.add(new StringRefAddr("maxActive", "4"));
   ref.add(new StringRefAddr("maxWait", "5000"));
   ref.add(new StringRefAddr("removeAbandoned", "true"));
   ref.add(new StringRefAddr("removeAbandonedTimeout", "5000"));
   jndiName = props.getProperty("jndi.name");
   rebind(ic, jndiName, ref);
   ic.rebind(jndiName, ref);
 }
 @Before
 public void generalSetUp() throws Exception {
   InitialContext ic = new InitialContext();
   ic.rebind(JNDINames.ATTACHMENT_DATASOURCE, dataSource);
   IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
   DatabaseOperation.DELETE_ALL.execute(connection, dataSet);
   DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
   DBUtil.getInstanceForTest(dataSource.getConnection());
   if (!registred) {
     Reader reader = null;
     try {
       reader =
           new InputStreamReader(
               SimpleFileAccessControlTest.class
                   .getClassLoader()
                   .getResourceAsStream("silverpeas-jcr.txt"),
               CharEncoding.UTF_8);
       SilverpeasRegister.registerNodeTypes(reader);
     } finally {
       IOUtils.closeQuietly(reader);
     }
     registred = true;
     DBUtil.getInstanceForTest(dataSource.getConnection());
   }
   Session session = null;
   try {
     session = getRepository().login(new SilverpeasSystemCredentials());
     if (!session.getRootNode().hasNode(instanceId)) {
       session.getRootNode().addNode(instanceId, NT_FOLDER);
     }
     session.save();
   } finally {
     if (session != null) {
       session.logout();
     }
   }
 }
Пример #13
0
  public MultipleConnection(String[] args) throws Exception {
    if (args.length != 2) {
      System.out.println(USAGE);
      System.exit(1);
    }

    // first, load the properties from the spy.properties file
    Properties prop = new Properties();
    try {
      prop.load(ClassLoader.getSystemResourceAsStream("spy.properties"));
    } catch (Exception e) {
      System.err.println("problem to load properties.");
      e.printStackTrace();
      System.exit(1);
    }

    login = prop.getProperty("login");
    password = prop.getProperty("password");
    url = prop.getProperty("url");
    driver = prop.getProperty("driver");

    // completion is either "commit", either "rollback"
    String completion = args[0];

    // get the new value which will be assign to the database
    int newValue = 0;
    try {
      newValue = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
      System.err.println(USAGE);
      System.err.println("[number] has to be an integer\n");
      System.exit(1);
    }

    // Get a transaction manager
    try {
      // creates an instance of JOTM with a local transaction factory which is not bound to a
      // registry
      jotm = new Jotm(true, false);
      InitialContext ictx = new InitialContext();
      ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction());
    } catch (Exception e) {
      System.err.println("JOTM problem.");
      e.printStackTrace();
      System.exit(1);
    }

    // get an user transaction
    UserTransaction utx = null;
    try {
      System.out.println("create initial context");
      Context ictx = new InitialContext();
      System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME);
      utx = (UserTransaction) ictx.lookup(USER_TRANSACTION_JNDI_NAME);
    } catch (Exception e) {
      System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
      System.err.println("Exception message :" + e.getMessage());
      e.printStackTrace();
      System.exit(1);
    }

    // create an XA pool datasource with a minimum of 4 objects
    StandardXAPoolDataSource spds = new StandardXAPoolDataSource(4);
    spds.setMaxSize(15);
    spds.setMinSize(13);
    spds.setUser(login);
    spds.setPassword(password);

    // create an XA datasource which will be given to the XA pool
    StandardXADataSource xads = new StandardXADataSource();
    try {
      xads.setDriverName(driver);
      xads.setUrl(url);
      xads.setUser(login);
      xads.setPassword(password);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    // give the XA datasource to the pool (to create futur objects)
    spds.setTransactionManager(jotm.getTransactionManager());
    spds.setDataSource(xads);

    try {
      System.out.println("** begin a transaction **");
      utx.begin();

      conn = spds.getConnection(login, password);
      PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY);
      pstmt.setInt(1, newValue);
      pstmt.executeUpdate();
      System.out.println("dump, after the first update:");
      printTable();
      pstmt.close();
      conn.close();

      conn = spds.getConnection(login, password);
      PreparedStatement pstmt2 = conn.prepareStatement(SQL_QUERY);
      pstmt2.setInt(1, newValue + 2);
      pstmt2.executeUpdate();
      System.out.println("dump, after the second update:");
      printTable();
      pstmt2.close();
      conn.close();

      if (completion.equals("commit")) {
        System.out.println("** commit ** the transaction");
        utx.commit();
      } else {
        System.out.println("** rollback ** the transaction");
        utx.rollback();
      }
    } catch (Exception e) {
      System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
      System.err.println("Exception message :" + e.getMessage());
      e.printStackTrace();
      System.exit(1);
    }

    System.out.println("dump, after work:");
    conn = spds.getConnection(login, password);
    printTable();
    conn.close();
    stop();
  }
Пример #14
0
  protected void setUp() throws Exception {
    // set up db stuff
    super.setUp();

    // create bpel definition
    BpelProcessDefinition processDefinition =
        new BpelProcessDefinition("testProcess", BpelConstants.NS_EXAMPLES);

    definition =
        WsdlUtil.getFactory().newWSDLReader().readWSDL("", WsdlConverterTest.transform(WSDL_TEXT));
    ImportDefinition importDefinition = processDefinition.getImportDefinition();
    importDefinition.addImport(WsdlUtil.createImport(definition));
    new BpelReader().registerPropertyAliases(importDefinition);

    // partner link type
    PartnerLinkType partnerLinkType =
        importDefinition.getPartnerLinkType(new QName(BpelConstants.NS_EXAMPLES, "plt"));

    // rpc partner link
    PartnerLinkDefinition rpcPartnerLink = new PartnerLinkDefinition();
    rpcPartnerLink.setName("rpcPl");
    rpcPartnerLink.setPartnerLinkType(partnerLinkType);
    rpcPartnerLink.setMyRole(partnerLinkType.getFirstRole());

    // doc partner link
    PartnerLinkDefinition docPartnerLink = new PartnerLinkDefinition();
    docPartnerLink.setName("docPl");
    docPartnerLink.setPartnerLinkType(partnerLinkType);
    docPartnerLink.setMyRole(partnerLinkType.getSecondRole());

    // global scope
    Scope globalScope = processDefinition.getGlobalScope();
    globalScope.addPartnerLink(rpcPartnerLink);
    globalScope.addPartnerLink(docPartnerLink);
    globalScope.setActivity(new Empty());

    // deploy process definition
    bpelGraphSession.deployProcessDefinition(processDefinition);
    // save generated plink id
    rpcPartnerLinkId = rpcPartnerLink.getId();
    docPartnerLinkId = docPartnerLink.getId();

    // create application descriptor
    DeploymentDescriptor deploymentDescriptor = new DeploymentDescriptor();
    deploymentDescriptor.setName(processDefinition.getName());

    InitialContext initialContext = new InitialContext();
    try {
      // link jms administered objects
      initialContext.rebind(
          IntegrationControl.CONNECTION_FACTORY_NAME, new LinkRef("ConnectionFactory"));
      initialContext.rebind("rpcPl", new LinkRef("queue/testQueue"));
      initialContext.rebind("docPl", new LinkRef("queue/testQueue"));

      // configure relation context
      integrationControl =
          JmsIntegrationServiceFactory.getConfigurationInstance(jbpmConfiguration)
              .getIntegrationControl(processDefinition);
      integrationControl.setDeploymentDescriptor(deploymentDescriptor);
      // bind port entries and lookup destinations
      IntegrationControlHelper.setUp(integrationControl, jbpmContext);

      // unlink jms administered objects
      initialContext.unbind(IntegrationControl.CONNECTION_FACTORY_NAME);
      initialContext.unbind("rpcPl");
      initialContext.unbind("docPl");
    } finally {
      initialContext.close();
    }
  }