Java Code Examples for javax.jdo.JDOHelper

The following examples show how to use javax.jdo.JDOHelper. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Alpine   Source File: PersistenceManagerFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
    LOGGER.info("Initializing persistence framework");

    final String driverPath = Config.getInstance().getProperty(Config.AlpineKey.DATABASE_DRIVER_PATH);
    if (driverPath != null) {
        final JavaVersion jv = new JavaVersion();
        if (jv.getMajor() > 8) {
            LOGGER.warn("Cannot dynamically expand classpath to include database driver. This capability "
                    + "was removed in Java 11 and higher. Please add the database driver to the classpath "
                    + "when starting the application.");
        } else {
            Config.getInstance().expandClasspath(driverPath);
        }
    }

    pmf = (JDOPersistenceManagerFactory)JDOHelper.getPersistenceManagerFactory(JdoProperties.get(), "Alpine");

    // Ensure that the UpgradeMetaProcessor and SchemaVersion tables are created NOW, not dynamically at runtime.
    final PersistenceNucleusContext ctx = pmf.getNucleusContext();
    final Set<String> classNames = new HashSet<>();
    classNames.add(InstalledUpgrades.class.getCanonicalName());
    classNames.add(SchemaVersion.class.getCanonicalName());
    ((SchemaAwareStoreManager)ctx.getStoreManager()).createSchemaForClasses(classNames, new Properties());
}
 
Example 2
Source Project: seldon-server   Source File: JdoPeerUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static <T> void updateRetrievedItem(Class<T> clazz, T candidateObject, T retrievedObject) {
    final Field[] itemFields = clazz.getDeclaredFields();
    for (Field itemField : itemFields) {
        itemField.setAccessible(true);
        try {
            final Object submissionFieldValue = itemField.get(candidateObject);
            if (submissionFieldValue != null) {
                final String fieldName = itemField.getName();
                if (fieldName.startsWith("jdo")) {
                    continue;
                }
                logger.info("Setting retrieved candidateObject's " + fieldName + " to " + submissionFieldValue);
                itemField.set(retrievedObject, submissionFieldValue);
                JDOHelper.makeDirty(retrievedObject, fieldName);
            }
        } catch (IllegalAccessException e) {
            logger.info(e.getMessage());
        }
    }
}
 
Example 3
Source Project: estatio   Source File: IncomingInvoiceRepository.java    License: Apache License 2.0 6 votes vote down vote up
@Programmatic
public boolean findCreditNotesInStateOf(
        final BankAccount creditorBankAccount,
        final List<IncomingInvoiceApprovalState> upstreamStates) {
    final PersistenceManager jdoPersistenceManager = isisJdoSupport.getJdoPersistenceManager();
    final String sql = constructSqlForCreditNotesInStateOf(upstreamStates);
    final Query query = jdoPersistenceManager.newQuery("javax.jdo.query.SQL", sql);
    try {
        query.setResultClass(Integer.class);
        final DatastoreIdImpl datastoreId = (DatastoreIdImpl) JDOHelper.getObjectId(creditorBankAccount);
        final long idKeyAsObject = (long) datastoreId.getKeyAsObject();
        final ForwardQueryResult result = (ForwardQueryResult) query.executeWithMap(ImmutableMap.of("bankAccountId", idKeyAsObject));
        return !result.isEmpty();
    } finally {
        if (query!=null) query.closeAll();
    }
}
 
Example 4
Source Project: Alpine   Source File: PersistenceManagerFactory.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new JDO PersistenceManager.
 * @return a PersistenceManager
 */
public static PersistenceManager createPersistenceManager() {
    if (Config.isUnitTestsEnabled()) {
        pmf = (JDOPersistenceManagerFactory)JDOHelper.getPersistenceManagerFactory(JdoProperties.unit(), "Alpine");
    }
    if (pmf == null) {
        throw new IllegalStateException("Context is not initialized yet.");
    }
    return pmf.getPersistenceManager();
}
 
Example 5
Source Project: seldon-server   Source File: JDOFactory.java    License: Apache License 2.0 5 votes vote down vote up
private void registerFactory(String clientName, String databaseName, DataSource ds) {
  	Properties connectionProperties = (Properties) dataNucleusProperties.clone();
  	connectionProperties.put("javax.jdo.option.ConnectionFactory", ds);
if (databaseName != null)
  		connectionProperties.setProperty("datanucleus.mapping.Catalog", databaseName);
  	logger.info("Adding PMF factory for client "+clientName+" with database "+databaseName);
      PersistenceManagerFactory factory = JDOHelper.getPersistenceManagerFactory(connectionProperties);
      factories.put(clientName, factory);
  }
 
Example 6
/**
 * Gets the singleton instance of the PersistenceManager
 * 
 * @return the singleton instance of the PersistenceManager
 */
public static PersistenceManager getPersistenceManager() {
  if (instance == null) {
    instance =
        JDOHelper.getPersistenceManagerFactory("transactions-optional")
            .getPersistenceManager();
  }
  return instance;
}
 
Example 7
Source Project: estatio   Source File: UdoDomainObject.java    License: Apache License 2.0 5 votes vote down vote up
@Programmatic
public String getId() {
    Object objectId = JDOHelper.getObjectId(this);
    if (objectId == null) {
        return "";
    }
    String objectIdStr = objectId.toString();
    final String id = objectIdStr.split("\\[OID\\]")[0];
    return id;
}
 
Example 8
Source Project: estatio   Source File: CommunicationChannel.java    License: Apache License 2.0 5 votes vote down vote up
@Programmatic
public String getId() {
    Object objectId = JDOHelper.getObjectId(this);
    if (objectId == null) {
        return "";
    }
    String objectIdStr = objectId.toString();
    final String id = objectIdStr.split("\\[OID\\]")[0];
    return id;
}
 
Example 9
Source Project: estatio   Source File: Communication.java    License: Apache License 2.0 5 votes vote down vote up
@Programmatic
public String getId() {
    Object objectId = JDOHelper.getObjectId(this);
    if (objectId == null) {
        return "";
    }
    String objectIdStr = objectId.toString();
    final String id = objectIdStr.split("\\[OID\\]")[0];
    return id;
}
 
Example 10
Source Project: estatio   Source File: CommChannelRole.java    License: Apache License 2.0 5 votes vote down vote up
@Programmatic
public String getId() {
    Object objectId = JDOHelper.getObjectId(this);
    if (objectId == null) {
        return "";
    }
    String objectIdStr = objectId.toString();
    final String id = objectIdStr.split("\\[OID\\]")[0];
    return id;
}
 
Example 11
Source Project: estatio   Source File: DocumentAbstract.java    License: Apache License 2.0 5 votes vote down vote up
@Programmatic
public String getId() {
    Object objectId = JDOHelper.getObjectId(this);
    if (objectId == null) {
        return "";
    }
    String objectIdStr = objectId.toString();
    final String id = objectIdStr.split("\\[OID\\]")[0];
    return id;
}
 
Example 12
@Test
public void when_italian_tax_receipt() throws Exception {

    // given
    List<Document> incomingDocumentsBefore = repository.findIncomingDocuments();
    assertThat(incomingDocumentsBefore).isEmpty();

    // when
    final String fileName = "some_tax_receipt.pdf";
    final byte[] pdfBytes = Resources.toByteArray(
            Resources.getResource(DocumentServiceRestApi_uploadGeneric_IntegTest.class, fileName));
    final Blob blob = new Blob(fileName, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes);

    wrap(documentService).uploadGeneric(blob, "INCOMING_TAX_REGISTER", "/ITA");

    transactionService.nextTransaction();

    // then
    List<Document> incomingDocumentsAfter = repository.findAllIncomingDocuments();
    assertThat(incomingDocumentsAfter).hasSize(1);

    Document document = incomingDocumentsAfter.get(0);
    Blob documentBlob = document.getBlob();

    assertThat(document.getAtPath()).isEqualTo("/ITA");
    assertThat(documentBlob.getName()).isEqualTo(blob.getName());
    assertThat(documentBlob.getMimeType().getBaseType()).isEqualTo(blob.getMimeType().getBaseType());
    assertThat(documentBlob.getBytes()).isEqualTo(blob.getBytes());
    assertThat(JDOHelper.getVersion(document)).isEqualTo(1L);
    assertThat(document.getType()).isEqualTo(DocumentTypeData.INCOMING_TAX_REGISTER.findUsing(documentTypeRepository));

}
 
Example 13
Source Project: incubator-sentry   Source File: TestSentryRole.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  dataDir = new File(Files.createTempDir(), "sentry_policy_db");
  Properties prop = new Properties();
  prop.setProperty(ServerConfig.JAVAX_JDO_URL, "jdbc:derby:;databaseName=" + dataDir.getPath() + ";create=true");
  prop.setProperty(ServerConfig.JAVAX_JDO_USER, "Sentry");
  prop.setProperty(ServerConfig.JAVAX_JDO_PASS, "Sentry");
  prop.setProperty(ServerConfig.JAVAX_JDO_DRIVER_NAME, "org.apache.derby.jdbc.EmbeddedDriver");
  prop.setProperty("datanucleus.autoCreateSchema", "true");
  prop.setProperty("datanucleus.fixedDatastore", "false");
  prop.setProperty("datanucleus.NontransactionalRead", "false");
  prop.setProperty("datanucleus.NontransactionalWrite", "false");
  pmf = JDOHelper.getPersistenceManagerFactory(prop);
}
 
Example 14
Source Project: dependency-track   Source File: UpgradeInitializer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void contextInitialized(final ServletContextEvent event) {
    LOGGER.info("Initializing upgrade framework");

    final String driverPath = Config.getInstance().getProperty(Config.AlpineKey.DATABASE_DRIVER_PATH);
    if (driverPath != null) {
        Config.getInstance().expandClasspath(driverPath);
    }
    final JDOPersistenceManagerFactory pmf  = (JDOPersistenceManagerFactory) JDOHelper.getPersistenceManagerFactory(JdoProperties.get(), "Alpine");

    // Ensure that the UpgradeMetaProcessor and SchemaVersion tables are created NOW, not dynamically at runtime.
    final PersistenceNucleusContext ctx = pmf.getNucleusContext();
    final Set<String> classNames = new HashSet<>();
    classNames.add(InstalledUpgrades.class.getCanonicalName());
    classNames.add(SchemaVersion.class.getCanonicalName());
    ((SchemaAwareStoreManager)ctx.getStoreManager()).createSchemaForClasses(classNames, new Properties());

    if (RequirementsVerifier.failedValidation()) {
        return;
    }
    try (final PersistenceManager pm = pmf.getPersistenceManager();
         final QueryManager qm = new QueryManager(pm)) {
        final UpgradeExecutor executor = new UpgradeExecutor(qm);
        try {
            executor.executeUpgrades(UpgradeItems.getUpgradeItems());
        } catch (UpgradeException e) {
            LOGGER.error("An error occurred performing upgrade processing. " + e.getMessage());
        }
    }
    pmf.close();
}
 
Example 15
public JdoObjectRetrievalFailureException(JDOObjectNotFoundException ex) {
	// Extract information about the failed object from the JDOException, if available.
	super((ex.getFailedObject() != null ? ex.getFailedObject().getClass() : null),
			(ex.getFailedObject() != null ? JDOHelper.getObjectId(ex.getFailedObject()) : null),
			ex.getMessage(), ex);
}
 
Example 16
public JdoOptimisticLockingFailureException(JDOOptimisticVerificationException ex) {
	// Extract information about the failed object from the JDOException, if available.
	super((ex.getFailedObject() != null ? ex.getFailedObject().getClass() : null),
			(ex.getFailedObject() != null ? JDOHelper.getObjectId(ex.getFailedObject()) : null),
			ex.getMessage(), ex);
}
 
Example 17
public JdoObjectRetrievalFailureException(JDOObjectNotFoundException ex) {
	// Extract information about the failed object from the JDOException, if available.
	super((ex.getFailedObject() != null ? ex.getFailedObject().getClass() : null),
			(ex.getFailedObject() != null ? JDOHelper.getObjectId(ex.getFailedObject()) : null),
			ex.getMessage(), ex);
}
 
Example 18
public JdoOptimisticLockingFailureException(JDOOptimisticVerificationException ex) {
	// Extract information about the failed object from the JDOException, if available.
	super((ex.getFailedObject() != null ? ex.getFailedObject().getClass() : null),
			(ex.getFailedObject() != null ? JDOHelper.getObjectId(ex.getFailedObject()) : null),
			ex.getMessage(), ex);
}
 
Example 19
Source Project: hyperjaxb3   Source File: RoundtripTest.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
	protected void checkSample(File sample) throws Exception {
		// TODO Auto-generated method stub
		final JAXBContext context = createContext();
		logger.debug("Unmarshalling.");
		final Unmarshaller unmarshaller = context.createUnmarshaller();
		// Unmarshall the document
		final JAXBElement element = (JAXBElement) unmarshaller
				.unmarshal(sample);
		final Object object = element.getValue();
		logger.debug("Opening session.");
		// Open the session, save object into the database
		logger.debug("Saving the object.");
		final PersistenceManager saveManager = createPersistenceManager();
//		saveManager.setDetachAllOnCommit(true);
		final Transaction saveTransaction = saveManager.currentTransaction();
		saveTransaction.setNontransactionalRead(true);
		saveTransaction.begin();
		// final Object merged = saveSession.merge(object);
		// saveSession.replicate(object, ReplicationMode.OVERWRITE);
		// saveSession.get
		// final Serializable id =
		final Object mergedObject = saveManager.makePersistent(object);
		
//		final Object asd = saveManager.detachCopy(object);
		saveTransaction.commit();
//		final Object id = saveManager.getObjectId(mergedObject);
		final Object identity = JDOHelper.getObjectId(object);
		final Object id = identity instanceof SingleFieldIdentity ? ((SingleFieldIdentity) identity).getKeyAsObject() : identity;
		// Close the session
		saveManager.close();

		logger.debug("Opening session.");
		// Open the session, load the object
		final PersistenceManager loadManager = createPersistenceManager();
		final Transaction loadTransaction = loadManager.currentTransaction();
		loadTransaction.setNontransactionalRead(true);
		logger.debug("Loading the object.");
		final Object loadedObject = loadManager.getObjectById(mergedObject.getClass(), id);
		logger.debug("Closing the session.");

		final JAXBElement mergedElement = new JAXBElement(element.getName(),
				element.getDeclaredType(), object);

		final JAXBElement loadedElement = new JAXBElement(element.getName(),
				element.getDeclaredType(), loadedObject);

		logger.debug("Checking the document identity.");

		logger.debug("Source object:\n"
				+ ContextUtils.toString(context, mergedElement));
		logger.debug("Result object:\n"
				+ ContextUtils.toString(context, loadedElement));

		checkObjects(mergedObject, loadedObject);
		loadManager.close();

	}
 
Example 20
Source Project: estatio   Source File: DocumentTemplate.java    License: Apache License 2.0 4 votes vote down vote up
@Programmatic
private long getVersion() {
    return (Long)JDOHelper.getVersion(this);
}
 
Example 21
Source Project: estatio   Source File: DocumentMenu_Upload_IntegTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void happy_case() throws Exception {

    // given
    List<Document> incomingDocumentsBefore = repository.findIncomingDocuments();
    assertThat(incomingDocumentsBefore).isEmpty();

    // when
    final String fileName = "3020100123.pdf";
    final byte[] pdfBytes = Resources.toByteArray(
            Resources.getResource(DocumentMenu_Upload_IntegTest.class, fileName));
    final Blob blob = new Blob(fileName, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes);
    queryResultsCache.resetForNextTransaction(); // workaround: clear MeService#me cache
    final Document uploadedDocument1 = sudoService
            .sudo(Person_enum.DanielOfficeAdministratorFr.getRef().toLowerCase(), () ->
                    wrap(documentMenu).upload(blob));
    transactionService.nextTransaction();

    // then
    List<Document> incomingDocumentsAfter = repository.findAllIncomingDocuments();
    assertThat(incomingDocumentsAfter).hasSize(1);
    final Document document = incomingDocumentsAfter.get(0);
    assertThat(document).isSameAs(uploadedDocument1);

    final Blob documentBlob = uploadedDocument1.getBlob();

    assertThat(ApplicationTenancy_enum.FrBe.getPath()).isEqualTo("/FRA;/BEL");
    assertThat(uploadedDocument1.getAtPath()).isEqualTo("/FRA");
    assertThat(documentBlob.getName()).isEqualTo(blob.getName());
    assertThat(documentBlob.getMimeType().getBaseType()).isEqualTo(blob.getMimeType().getBaseType());
    assertThat(documentBlob.getBytes()).isEqualTo(blob.getBytes());
    assertThat(JDOHelper.getVersion(uploadedDocument1)).isEqualTo(1L);
    assertThat(paperclipRepository.findByDocument(uploadedDocument1).size()).isEqualTo(0);

    // and then also
    final List<IncomingDocumentCategorisationStateTransition> transitions =
            stateTransitionRepository.findByDomainObject(document);

    assertThat(transitions).hasSize(2);
    assertTransition(transitions.get(0),
            NEW, CATEGORISE, null);
    assertTransition(transitions.get(1),
            null, INSTANTIATE, NEW);

    // and when
    final String fileName2 = "3020100123-altered.pdf";
    final byte[] pdfBytes2 = Resources.toByteArray(
            Resources.getResource(DocumentMenu_Upload_IntegTest.class, fileName2));
    final Blob similarNamedBlob = new Blob(fileName, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes2);
    final Document uploadedDocument2 = wrap(documentMenu).upload(similarNamedBlob);
    transactionService.nextTransaction();


    // then
    assertThat(uploadedDocument1).isNotSameAs(uploadedDocument2);

    incomingDocumentsAfter = repository.findAllIncomingDocuments();
    assertThat(incomingDocumentsAfter).hasSize(2);
    assertThat(incomingDocumentsAfter).contains(uploadedDocument1, uploadedDocument2);

    assertThat(JDOHelper.getVersion(uploadedDocument2)).isEqualTo(1L);
    assertThat(uploadedDocument2.getName()).isEqualTo(fileName);
    assertThat(uploadedDocument2.getBlobBytes()).isEqualTo(similarNamedBlob.getBytes());

    final List<Paperclip> paperclipByAttachedTo = paperclipRepository.findByAttachedTo(uploadedDocument2);
    assertThat(paperclipByAttachedTo.size()).isEqualTo(1);
    final List<Paperclip> paperclipByDocument = paperclipRepository.findByDocument(uploadedDocument1);
    assertThat(paperclipByDocument.size()).isEqualTo(1);
    assertThat(paperclipByDocument.get(0)).isSameAs(paperclipByAttachedTo.get(0));
    assertThat(paperclipRepository.findByAttachedTo(uploadedDocument1)).isEmpty();

    assertThat(JDOHelper.getVersion(uploadedDocument1)).isEqualTo(2L);
    assertThat(uploadedDocument1.getName()).startsWith("arch");
    assertThat(uploadedDocument1.getName()).endsWith(fileName);
    assertThat(uploadedDocument1.getBlobBytes()).isEqualTo(documentBlob.getBytes());

    // and when since ECP-676 atPath derived from filename for France and Belgium
    final String belgianBarCode = "6010012345.pdf";
    final Blob belgianBlob = new Blob(belgianBarCode, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes2);
    Document belgianDocument = wrap(documentMenu).upload(belgianBlob);

    // then
    assertThat(belgianDocument.getAtPath()).isEqualTo("/BEL");

}
 
Example 22
@Test
public void when_incoming_document() throws Exception {

    // given
    List<Document> incomingDocumentsBefore = repository.findIncomingDocuments();
    assertThat(incomingDocumentsBefore).isEmpty();

    // when
    final String fileName = "3020100123.pdf";
    final byte[] pdfBytes = Resources.toByteArray(
            Resources.getResource(DocumentServiceRestApi_uploadGeneric_IntegTest.class, fileName));
    final Blob blob = new Blob(fileName, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes);
    queryResultsCache.resetForNextTransaction(); // workaround: clear MeService#me cache

    wrap(documentService).uploadGeneric(blob, "INCOMING", "/FRA");
    transactionService.nextTransaction();

    // then
    List<Document> incomingDocumentsAfter = repository.findAllIncomingDocuments();
    assertThat(incomingDocumentsAfter).hasSize(1);

    final Document document = incomingDocumentsAfter.get(0);
    final Blob documentBlob = document.getBlob();

    assertThat(document.getAtPath()).isEqualTo("/FRA");
    assertThat(documentBlob.getName()).isEqualTo(blob.getName());
    assertThat(documentBlob.getMimeType().getBaseType()).isEqualTo(blob.getMimeType().getBaseType());
    assertThat(documentBlob.getBytes()).isEqualTo(blob.getBytes());
    assertThat(JDOHelper.getVersion(document)).isEqualTo(1L);
    assertThat(paperclipRepository.findByDocument(document).size()).isEqualTo(0);

    // and then also
    final List<IncomingDocumentCategorisationStateTransition> transitions =
            stateTransitionRepository.findByDomainObject(document);

    assertThat(transitions).hasSize(2);
    assertTransition(transitions.get(0),
            NEW, CATEGORISE, null);
    assertTransition(transitions.get(1),
            null, INSTANTIATE, NEW);

    // and when
    final String fileName2 = "3020100123-altered.pdf";
    final byte[] pdfBytes2 = Resources.toByteArray(
            Resources.getResource(DocumentServiceRestApi_uploadGeneric_IntegTest.class, fileName2));
    final Blob similarNamedBlob = new Blob(fileName, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes2);

    wrap(documentService).uploadGeneric(similarNamedBlob, "INCOMING", "/FRA");

    transactionService.nextTransaction();

    // then
    incomingDocumentsAfter = repository.findAllIncomingDocuments();
    assertThat(incomingDocumentsAfter).hasSize(2);
    assertThat(incomingDocumentsAfter.get(0).getName()).startsWith("arch");
    assertThat(incomingDocumentsAfter.get(0).getName()).endsWith(fileName);
    assertThat(incomingDocumentsAfter.get(0).getBlobBytes()).isEqualTo(documentBlob.getBytes());

    assertThat(JDOHelper.getVersion(incomingDocumentsAfter.get(0))).isEqualTo(2L);
    assertThat(paperclipRepository.findByDocument(incomingDocumentsAfter.get(0)).size()).isEqualTo(1);
    assertThat(incomingDocumentsAfter.get(1).getName()).isEqualTo(fileName);
    assertThat(incomingDocumentsAfter.get(1).getBlobBytes()).isEqualTo(similarNamedBlob.getBytes());
    assertThat(JDOHelper.getVersion(incomingDocumentsAfter.get(1))).isEqualTo(1L);
    assertThat(paperclipRepository.findByDocument(incomingDocumentsAfter.get(1)).size()).isEqualTo(0);

    final String belgianBarCode = "6010012345.pdf";
    final Blob belgianBlob = new Blob(belgianBarCode, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes2);
    Document belgianDocument = wrap(documentService).uploadGeneric(belgianBlob, "INCOMING", "/FRA");

    // then
    assertThat(belgianDocument.getAtPath()).isEqualTo("/BEL");

}
 
Example 23
@Test
public void when_incoming_italian_document() throws Exception {

    // given
    List<Document> incomingDocumentsBefore = repository.findIncomingDocuments();
    assertThat(incomingDocumentsBefore).isEmpty();

    // when
    final String fileName = "2010101234.pdf";
    final byte[] pdfBytes = Resources.toByteArray(
            Resources.getResource(DocumentServiceRestApi_uploadGeneric_IntegTest.class, fileName));
    final Blob blob = new Blob(fileName, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes);
    final Document document1 = wrap(documentService).uploadGeneric(blob, "INCOMING", "/ITA");
    transactionService.nextTransaction();

    // then
    List<Document> incomingDocumentsAfter = repository.findAllIncomingDocuments();
    assertThat(incomingDocumentsAfter).hasSize(1);

    final Document document = incomingDocumentsAfter.get(0);
    assertThat(document1).isSameAs(document);

    final Blob documentBlob = document1.getBlob();

    assertThat(document1.getAtPath()).isEqualTo("/ITA");
    assertThat(documentBlob.getName()).isEqualTo(blob.getName());
    assertThat(documentBlob.getMimeType().getBaseType()).isEqualTo(blob.getMimeType().getBaseType());
    assertThat(documentBlob.getBytes()).isEqualTo(blob.getBytes());
    assertThat(JDOHelper.getVersion(document1)).isEqualTo(1L);
    assertThat(document1.getType()).isEqualTo(DocumentTypeData.INCOMING.findUsing(documentTypeRepository));

    // and then also
    assertThat(stateTransitionRepository.findByDomainObject(document1)).hasSize(0);
    // TODO: when implemented assert link between doc and inc inv

    // and when again uploading doc with the same name (but different bytes)
    final byte[] pdfBytes2 = Resources.toByteArray(
            Resources.getResource(DocumentServiceRestApi_uploadGeneric_IntegTest.class, "2010101234-altered.pdf"));
    final Blob similarNamedBlobWithDifferentBytes = new Blob(fileName, MimeTypeData.APPLICATION_PDF.asStr(), pdfBytes2);

    final Document document2 = wrap(documentService)
            .uploadGeneric(similarNamedBlobWithDifferentBytes, "INCOMING", "/ITA");
    transactionService.nextTransaction();

    // then
    incomingDocumentsAfter = repository.findAllIncomingDocuments();
    assertThat(incomingDocumentsAfter).hasSize(2);
    assertThat(incomingDocumentsAfter).contains(document1, document2);

    final Blob document2Blob = document2.getBlob();

    assertThat(document2.getAtPath()).isEqualTo("/ITA");

    assertThat(document2Blob.getName()).isEqualTo(fileName);
    assertThat(document2Blob.getMimeType().getBaseType()).isEqualTo(similarNamedBlobWithDifferentBytes.getMimeType().getBaseType());
    assertThat(document2Blob.getBytes()).isEqualTo(similarNamedBlobWithDifferentBytes.getBytes());

    final Blob document1Blob = document1.getBlob();

    assertThat(document1.getAtPath()).isEqualTo("/ITA");
    assertThat(document1Blob.getName()).startsWith("arch");
    assertThat(document1Blob.getName()).endsWith(fileName);

    assertThat(document1Blob.getMimeType().getBaseType()).isEqualTo(blob.getMimeType().getBaseType());
    assertThat(document1Blob.getBytes()).isEqualTo(blob.getBytes());
    assertThat(JDOHelper.getVersion(document1)).isEqualTo(2L);
    assertThat(document1.getType()).isEqualTo(DocumentTypeData.INCOMING.findUsing(documentTypeRepository));
}
 
Example 24
Source Project: incubator-sentry   Source File: SentryStore.java    License: Apache License 2.0 4 votes vote down vote up
public SentryStore(Configuration conf) throws SentryNoSuchObjectException,
SentryAccessDeniedException, SentryConfigurationException, IOException {
  commitSequenceId = 0;
  this.conf = conf;
  Properties prop = new Properties();
  prop.putAll(ServerConfig.SENTRY_STORE_DEFAULTS);
  String jdbcUrl = conf.get(ServerConfig.SENTRY_STORE_JDBC_URL, "").trim();
  Preconditions.checkArgument(!jdbcUrl.isEmpty(), "Required parameter " +
      ServerConfig.SENTRY_STORE_JDBC_URL + " is missed");
  String user = conf.get(ServerConfig.SENTRY_STORE_JDBC_USER, ServerConfig.
      SENTRY_STORE_JDBC_USER_DEFAULT).trim();
  //Password will be read from Credential provider specified using property
  // CREDENTIAL_PROVIDER_PATH("hadoop.security.credential.provider.path" in sentry-site.xml
  // it falls back to reading directly from sentry-site.xml
  char[] passTmp = conf.getPassword(ServerConfig.SENTRY_STORE_JDBC_PASS);
  String pass = null;
  if(passTmp != null) {
    pass = new String(passTmp);
  } else {
    throw new SentryConfigurationException("Error reading " + ServerConfig.SENTRY_STORE_JDBC_PASS);
  }

  String driverName = conf.get(ServerConfig.SENTRY_STORE_JDBC_DRIVER,
      ServerConfig.SENTRY_STORE_JDBC_DRIVER_DEFAULT);
  prop.setProperty(ServerConfig.JAVAX_JDO_URL, jdbcUrl);
  prop.setProperty(ServerConfig.JAVAX_JDO_USER, user);
  prop.setProperty(ServerConfig.JAVAX_JDO_PASS, pass);
  prop.setProperty(ServerConfig.JAVAX_JDO_DRIVER_NAME, driverName);
  for (Map.Entry<String, String> entry : conf) {
    String key = entry.getKey();
    if (key.startsWith(ServerConfig.SENTRY_JAVAX_JDO_PROPERTY_PREFIX) ||
        key.startsWith(ServerConfig.SENTRY_DATANUCLEUS_PROPERTY_PREFIX)) {
      key = StringUtils.removeStart(key, ServerConfig.SENTRY_DB_PROPERTY_PREFIX);
      prop.setProperty(key, entry.getValue());
    }
  }


  boolean checkSchemaVersion = conf.get(
      ServerConfig.SENTRY_VERIFY_SCHEM_VERSION,
      ServerConfig.SENTRY_VERIFY_SCHEM_VERSION_DEFAULT).equalsIgnoreCase(
          "true");
  if (!checkSchemaVersion) {
    prop.setProperty("datanucleus.autoCreateSchema", "true");
    prop.setProperty("datanucleus.fixedDatastore", "false");
  }

  // Disallow operations outside of transactions
  prop.setProperty("datanucleus.NontransactionalRead", "false");
  prop.setProperty("datanucleus.NontransactionalWrite", "false");

  pmf = JDOHelper.getPersistenceManagerFactory(prop);
  verifySentryStoreSchema(checkSchemaVersion);

  // Kick off the thread that cleans orphaned privileges (unless told not to)
  privCleaner = this.new PrivCleaner();
  if (conf.get(ServerConfig.SENTRY_STORE_ORPHANED_PRIVILEGE_REMOVAL,
          ServerConfig.SENTRY_STORE_ORPHANED_PRIVILEGE_REMOVAL_DEFAULT)
          .equalsIgnoreCase("true")) {
    privCleanerThread = new Thread(privCleaner);
    privCleanerThread.start();
  }
}
 
Example 25
Source Project: kite   Source File: TransformTask.java    License: Apache License 2.0 4 votes vote down vote up
public PipelineResult run() throws IOException {
  boolean isLocal = (isLocal(from.getDataset()) || isLocal(to.getDataset()));
  if (isLocal) {
    // copy to avoid making changes to the caller's configuration
    Configuration conf = new Configuration(getConf());
    conf.set("mapreduce.framework.name", "local");
    setConf(conf);
  }

  if (isHive(from) || isHive(to)) {
    setConf(addHiveDelegationToken(getConf()));

    // add jars needed for metastore interaction to the classpath
    if (!isLocal) {
      Class<?> fb303Class, thriftClass;
      try {
        // attempt to use libfb303 and libthrift 0.9.2 when async was added
        fb303Class = Class.forName(
            "com.facebook.fb303.FacebookService.AsyncProcessor");
        thriftClass = Class.forName(
            "org.apache.thrift.TBaseAsyncProcessor");
      } catch (ClassNotFoundException e) {
        try {
          // fallback to 0.9.0 or earlier
          fb303Class = Class.forName(
              "com.facebook.fb303.FacebookBase");
          thriftClass = Class.forName(
              "org.apache.thrift.TBase");
        } catch (ClassNotFoundException real) {
          throw new DatasetOperationException(
              "Cannot find thrift dependencies", real);
        }
      }

      TaskUtil.configure(getConf())
          .addJarForClass(Encoder.class) // commons-codec
          .addJarForClass(Log.class) // commons-logging
          .addJarForClass(CompressorInputStream.class) // commons-compress
          .addJarForClass(ApiAdapter.class) // datanucleus-core
          .addJarForClass(JDOAdapter.class) // datanucleus-api-jdo
          .addJarForClass(SQLQuery.class) // datanucleus-rdbms
          .addJarForClass(JDOHelper.class) // jdo-api
          .addJarForClass(Transaction.class) // jta
          .addJarForClass(fb303Class) // libfb303
          .addJarForClass(thriftClass) // libthrift
          .addJarForClass(HiveMetaStore.class) // hive-metastore
          .addJarForClass(HiveConf.class); // hive-exec
    }
  }

  PType<T> toPType = ptype(to);
  MapFn<T, T> validate = new CheckEntityClass<T>(to.getType());

  Pipeline pipeline = new MRPipeline(getClass(), getConf());

  PCollection<T> collection = pipeline.read(CrunchDatasets.asSource(from))
      .parallelDo(transform, toPType).parallelDo(validate, toPType);

  if (compact) {
    // the transform must be run before partitioning
    collection = CrunchDatasets.partition(collection, to, numWriters, numPartitionWriters);
  }

  pipeline.write(collection, CrunchDatasets.asTarget(to), mode);

  PipelineResult result = pipeline.done();

  StageResult sr = Iterables.getFirst(result.getStageResults(), null);
  if (sr != null && MAP_INPUT_RECORDS != null) {
    this.count = sr.getCounterValue(MAP_INPUT_RECORDS);
  }

  return result;
}
 
Example 26
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it for the specified name.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(String)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param name the name of the desired PersistenceManagerFactory
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(String)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(String name) {
	return JDOHelper.getPersistenceManagerFactory(name, this.beanClassLoader);
}
 
Example 27
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it via the given Properties
 * that got prepared by this LocalPersistenceManagerFactoryBean.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(Map)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param props the merged properties prepared by this LocalPersistenceManagerFactoryBean
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(java.util.Map)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(Map<?, ?> props) {
	return JDOHelper.getPersistenceManagerFactory(props, this.beanClassLoader);
}
 
Example 28
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it for the specified name.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(String)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param name the name of the desired PersistenceManagerFactory
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(String)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(String name) {
	return JDOHelper.getPersistenceManagerFactory(name, this.beanClassLoader);
}
 
Example 29
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it via the given Properties
 * that got prepared by this LocalPersistenceManagerFactoryBean.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(Map)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param props the merged properties prepared by this LocalPersistenceManagerFactoryBean
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(java.util.Map)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(Map<?, ?> props) {
	return JDOHelper.getPersistenceManagerFactory(props, this.beanClassLoader);
}
 
Example 30
Source Project: hyperjaxb3   Source File: AbstractJDOTest.java    License: BSD 2-Clause "Simplified" License 2 votes vote down vote up
protected PersistenceManagerFactory createPersistenceManagerFactory() {

    final Map properties = getPersistenceManagerFactoryProperties();

    return JDOHelper.getPersistenceManagerFactory(properties);
  }