use of javax.persistence.PersistenceException in project simplejpa by appoxy.
the class Save method getEnumValue.
static String getEnumValue(PersistentProperty field, Object ob) {
EnumType enumType = field.getEnumType();
Class retType = field.getPropertyClass();
Object propertyValue = field.getProperty(ob);
String toSet = null;
if (enumType == EnumType.STRING) {
toSet = propertyValue.toString();
} else {
// ordinal
Object[] enumConstants = retType.getEnumConstants();
for (int i = 0; i < enumConstants.length; i++) {
Object enumConstant = enumConstants[i];
if (enumConstant.equals(propertyValue)) {
toSet = Integer.toString(i);
break;
}
}
}
if (toSet == null) {
// should never happen
throw new PersistenceException("Enum value is null, couldn't find ordinal match: " + ob);
}
return toSet;
}
use of javax.persistence.PersistenceException in project simplejpa by appoxy.
the class LazyList method loadAtleastItems.
private synchronized void loadAtleastItems(int index) {
if ((backingList != null && nextToken == null) || (!noLimit() && index >= maxResults)) {
return;
}
if (backingList == null) {
backingList = new GrowthList();
}
while (backingList.size() <= index) {
SelectResult qr;
try {
if (logger.isLoggable(Level.FINER))
logger.finer("query for lazylist=" + origQuery);
int limit = maxResults - backingList.size();
String limitQuery = realQuery + " limit " + (noLimit() ? maxResultsPerToken : Math.min(maxResultsPerToken, limit));
if (em.getFactory().isPrintQueries())
System.out.println("query in lazylist=" + limitQuery);
qr = DomainHelper.selectItems(this.em.getSimpleDb(), limitQuery, nextToken, isConsistentRead());
if (logger.isLoggable(Level.FINER))
logger.finer("got items for lazylist=" + qr.getItems().size());
for (Item item : qr.getItems()) {
backingList.add((E) em.buildObject(genericReturnType, item.getName(), item.getAttributes()));
}
if (qr.getNextToken() == null || (!noLimit() && qr.getItems().size() == limit)) {
nextToken = null;
break;
}
if (!noLimit() && qr.getItems().size() > limit) {
throw new PersistenceException("Got more results than the limit.");
}
nextToken = qr.getNextToken();
} catch (AmazonClientException e) {
throw new PersistenceException("Query failed: Domain=" + domainName + " -> " + origQuery, e);
}
}
}
use of javax.persistence.PersistenceException in project simplejpa by appoxy.
the class EntityManagerFactoryImpl method getLibsToScan.
private static Set<String> getLibsToScan(Set<String> classNames) throws PersistenceException {
Set<String> libs = new HashSet<String>();
for (String className : classNames) {
try {
Class<?> clazz = Class.forName(className);
URL resource = clazz.getResource(clazz.getSimpleName() + ".class");
if (resource.getProtocol().equals("jar")) {
libs.add(resource.getFile().split("!")[0].substring(6));
} else if (resource.getProtocol().equals("file")) {
libs.add(resource.getFile().substring(1));
} else {
throw new PersistenceException("Unknown protocol in URL: " + resource);
}
} catch (Throwable e) {
throw new PersistenceException("Failed getting lib of class: " + className, e);
}
}
return libs;
}
use of javax.persistence.PersistenceException in project simplejpa by appoxy.
the class EntityManagerFactoryImpl method createClients.
private void createClients() {
AWSCredentials awsCredentials = null;
InputStream credentialsFile = getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties");
if (credentialsFile != null) {
logger.info("Loading credentials from AwsCredentials.properties");
try {
awsCredentials = new PropertiesCredentials(credentialsFile);
} catch (IOException e) {
throw new PersistenceException("Failed loading credentials from AwsCredentials.properties.", e);
}
} else {
logger.info("Loading credentials from simplejpa.properties");
String awsAccessKey = (String) this.props.get(AWSACCESS_KEY_PROP_NAME);
String awsSecretKey = (String) this.props.get(AWSSECRET_KEY_PROP_NAME);
if (awsAccessKey == null || awsAccessKey.length() == 0) {
throw new PersistenceException("AWS Access Key not found. It is a required property.");
}
if (awsSecretKey == null || awsSecretKey.length() == 0) {
throw new PersistenceException("AWS Secret Key not found. It is a required property.");
}
awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
}
this.simpleDbClient = new AmazonSimpleDBClient(awsCredentials, createConfiguration(sdbSecure));
this.simpleDbClient.setEndpoint(sdbEndpoint);
this.s3Client = new AmazonS3Client(awsCredentials, createConfiguration(s3Secure));
this.s3Client.setEndpoint(s3Endpoint);
}
use of javax.persistence.PersistenceException in project simplejpa by appoxy.
the class EntityManagerFactoryImpl method setupDbDomain.
public synchronized void setupDbDomain(String domainName) {
try {
if (!doesDomainExist(domainName)) {
logger.info("creating domain: " + domainName);
AmazonSimpleDB db = getSimpleDb();
db.createDomain(new CreateDomainRequest().withDomainName(domainName));
domainSet.add(domainName);
}
} catch (AmazonClientException e) {
throw new PersistenceException("Could not create SimpleDB domain.", e);
}
}
Aggregations