private static WriteConcern toWriteConcern( final String writeConcernConstant, final String writeConcernConstantClassName) { WriteConcern writeConcern; if (Strings.isNotEmpty(writeConcernConstant)) { if (Strings.isNotEmpty(writeConcernConstantClassName)) { try { final Class<?> writeConcernConstantClass = LoaderUtil.loadClass(writeConcernConstantClassName); final Field field = writeConcernConstantClass.getField(writeConcernConstant); writeConcern = (WriteConcern) field.get(null); } catch (final Exception e) { LOGGER.error( "Write concern constant [{}.{}] not found, using default.", writeConcernConstantClassName, writeConcernConstant); writeConcern = DEFAULT_WRITE_CONCERN; } } else { writeConcern = WriteConcern.valueOf(writeConcernConstant); if (writeConcern == null) { LOGGER.warn( "Write concern constant [{}] not found, using default.", writeConcernConstant); writeConcern = DEFAULT_WRITE_CONCERN; } } } else { writeConcern = DEFAULT_WRITE_CONCERN; } return writeConcern; }
/** Gets the write concern for entity or returns the default write concern for this datastore */ public WriteConcern getWriteConcern(Object clazzOrEntity) { WriteConcern wc = defConcern; if (clazzOrEntity != null) { Entity entityAnn = getMapper().getMappedClass(clazzOrEntity).getEntityAnnotation(); if (entityAnn != null && !"".equals(entityAnn.concern())) wc = WriteConcern.valueOf(entityAnn.concern()); } return wc; }
private void configureDs_() { List<Class<?>> pending = new ArrayList<Class<?>>(); Map<Class<?>, Integer> retries = new HashMap<Class<?>, Integer>(); List<ApplicationClass> cs = Play.classes.all(); for (ApplicationClass c : cs) { Class<?> clz = c.javaClass; if (clz.isAnnotationPresent(Entity.class)) { try { debug("mapping class: %1$s", clz.getName()); morphia_.map(clz); } catch (ConstraintViolationException e) { error(e, "error mapping class [%1$s]", clz); pending.add(clz); retries.put(clz, 1); } } } while (!pending.isEmpty()) { for (Class<?> clz : pending) { try { debug("mapping class: ", clz.getName()); morphia_.map(clz); pending.remove(clz); } catch (ConstraintViolationException e) { error(e, "error mapping class [%1$s]", clz); int retry = retries.get(clz); if (retry > 2) { throw new RuntimeException("too many errories mapping Morphia Entity classes"); } retries.put(clz, retries.get(clz) + 1); } } } ds().ensureIndexes(); String writeConcern = Play.configuration.getProperty("morphia.defaultWriteConcern", "safe"); if (null != writeConcern) { ds().setDefaultWriteConcern(WriteConcern.valueOf(writeConcern.toUpperCase())); } }
private WriteConcern extractWriteConcern(Exchange exchange) throws CamelMongoDbException { Object o = exchange.getIn().getHeader(MongoDbConstants.WRITECONCERN); if (o == null) { return null; } else if (o instanceof WriteConcern) { return ObjectHelper.cast(WriteConcern.class, o); } else if (o instanceof String) { WriteConcern answer = WriteConcern.valueOf(ObjectHelper.cast(String.class, o)); if (answer == null) { throw new CamelMongoDbException( "WriteConcern specified in the " + MongoDbConstants.WRITECONCERN + " header, with value " + o + " could not be resolved to a WriteConcern type"); } } // should never get here LOG.warn("A problem occurred while resolving the Exchange's Write Concern"); return null; }
/** * Set the {@link WriteConcern} for write operations on MongoDB using the standard ones. Resolved * from the fields of the WriteConcern class by calling the {@link WriteConcern#valueOf(String)} * method. * * @param writeConcern the standard name of the WriteConcern * @see <a * href="http://api.mongodb.org/java/current/com/mongodb/WriteConcern.html#valueOf(java.lang.String)">possible * options</a> */ public void setWriteConcern(String writeConcern) { this.writeConcern = WriteConcern.valueOf(writeConcern); }