List of usage examples for org.apache.commons.jxpath JXPathContext iterate
public abstract Iterator iterate(String xpath);
From source file:com.netspective.sparx.console.form.InspectObject.java
public void execute(Writer writer, DialogContext dc) throws IOException, DialogExecuteException { JXPathContext jxPathContext = null; DialogFieldStates fieldStates = dc.getFieldStates(); String contextValue = fieldStates.getState("context").getValue().getTextValue(); String jxPathExprValue = fieldStates.getState("jxpath-expr").getValue().getTextValue(); String action = fieldStates.getState("action").getValue().getTextValue(); if (contextValue.equalsIgnoreCase("Project")) jxPathContext = JXPathContext.newContext(dc.getProject()); else if (contextValue.equalsIgnoreCase("Servlet")) jxPathContext = JXPathContext.newContext(dc.getServlet()); else if (contextValue.equalsIgnoreCase("Application")) jxPathContext = JXPathServletContexts .getApplicationContext(dc.getServlet().getServletConfig().getServletContext()); else if (contextValue.equalsIgnoreCase("Request")) jxPathContext = JXPathServletContexts.getRequestContext(dc.getRequest(), dc.getServlet().getServletConfig().getServletContext()); else if (contextValue.equalsIgnoreCase("Session")) jxPathContext = JXPathServletContexts.getSessionContext(dc.getHttpRequest().getSession(), dc.getServlet().getServletConfig().getServletContext()); Object jxPathValue = null;//from w w w . ja va 2 s .co m if (action.equalsIgnoreCase("getValue")) jxPathValue = jxPathContext.getValue(jxPathExprValue); else jxPathValue = jxPathContext.iterate(jxPathExprValue); if (jxPathValue != null) { Map vars = new HashMap(); vars.put("jxPathValue", jxPathValue); vars.put("jxPathExpr", jxPathExprValue); inspectJxPathValueTemplate.process(writer, dc, vars); } else writer.write("JXPath expression '" + jxPathExprValue + "' evaluated to NULL."); }
From source file:io.jsondb.JsonDBTemplate.java
@SuppressWarnings("unchecked") @Override//w ww.jav a 2s. c o m public <T> T findOne(String jxQuery, String collectionName) { CollectionMetaData collectionMeta = cmdMap.get(collectionName); if ((null == collectionMeta) || (!collectionsRef.get().containsKey(collectionName))) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first"); } collectionMeta.getCollectionLock().readLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); Iterator<T> resultItr = context.iterate(jxQuery); while (resultItr.hasNext()) { T document = resultItr.next(); Object obj = Util.deepCopy(document); if (encrypted && collectionMeta.hasSecret() && null != obj) { CryptoUtil.decryptFields(obj, collectionMeta, dbConfig.getCipher()); } return (T) obj; // Return the first element we find. } return null; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); throw new JsonDBException( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); } finally { collectionMeta.getCollectionLock().readLock().unlock(); } }
From source file:io.jsondb.JsonDBTemplate.java
@SuppressWarnings("unchecked") @Override/*from w w w .ja v a2s .c om*/ public <T> List<T> find(String jxQuery, String collectionName) { CollectionMetaData cmd = cmdMap.get(collectionName); Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName); if ((null == cmd) || (null == collection)) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first."); } cmd.getCollectionLock().readLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); Iterator<T> resultItr = context.iterate(jxQuery); List<T> newCollection = new ArrayList<T>(); while (resultItr.hasNext()) { T document = resultItr.next(); Object obj = Util.deepCopy(document); if (encrypted && cmd.hasSecret() && null != obj) { CryptoUtil.decryptFields(obj, cmd, dbConfig.getCipher()); } newCollection.add((T) obj); } return newCollection; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); throw new JsonDBException( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); } finally { cmd.getCollectionLock().readLock().unlock(); } }
From source file:io.jsondb.JsonDBTemplate.java
@SuppressWarnings("unchecked") @Override//from www .j av a 2s . c om public <T> T findAndModify(String jxQuery, Update update, String collectionName) { CollectionMetaData cmd = cmdMap.get(collectionName); Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName); if ((null == cmd) || (null == collection)) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first."); } cmd.getCollectionLock().writeLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); Iterator<T> resultItr = context.iterate(jxQuery); T objectToModify = null; T clonedModifiedObject = null; while (resultItr.hasNext()) { objectToModify = resultItr.next(); break; // Use only the first element we find. } if (null != objectToModify) { //Clone it because we dont want to touch the in-memory object until we have really saved it clonedModifiedObject = (T) Util.deepCopy(objectToModify); for (Entry<String, Object> entry : update.getUpdateData().entrySet()) { Object newValue = Util.deepCopy(entry.getValue()); if (encrypted && cmd.hasSecret() && cmd.isSecretField(entry.getKey())) { newValue = dbConfig.getCipher().encrypt(newValue.toString()); } try { BeanUtils.copyProperty(clonedModifiedObject, entry.getKey(), newValue); } catch (IllegalAccessException | InvocationTargetException e) { logger.error( "Failed to copy updated data into existing collection document using BeanUtils", e); return null; } } Object idToModify = Util.getIdForEntity(clonedModifiedObject, cmd.getIdAnnotatedFieldGetterMethod()); JsonWriter jw = null; try { jw = new JsonWriter(dbConfig, cmd, collectionName, fileObjectsRef.get().get(collectionName)); } catch (IOException ioe) { logger.error("Failed to obtain writer for " + collectionName, ioe); throw new JsonDBException("Failed to save " + collectionName, ioe); } boolean updateResult = jw.updateInJsonFile(collection, idToModify, clonedModifiedObject); if (updateResult) { collection.put(idToModify, clonedModifiedObject); //Clone it once more because we want to disconnect it from the in-memory objects before returning. T returnObj = (T) Util.deepCopy(clonedModifiedObject); if (encrypted && cmd.hasSecret() && null != returnObj) { CryptoUtil.decryptFields(returnObj, cmd, dbConfig.getCipher()); } return returnObj; } } return null; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); throw new JsonDBException( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); } finally { cmd.getCollectionLock().writeLock().unlock(); } }
From source file:io.jsondb.JsonDBTemplate.java
@SuppressWarnings("unchecked") @Override//from w w w . j a v a 2 s.c om public <T> List<T> findAllAndModify(String jxQuery, Update update, String collectionName) { CollectionMetaData cmd = cmdMap.get(collectionName); Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName); if ((null == cmd) || (null == collection)) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first."); } cmd.getCollectionLock().writeLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); Iterator<T> resultItr = context.iterate(jxQuery); Map<Object, T> clonedModifiedObjects = new HashMap<Object, T>(); while (resultItr.hasNext()) { T objectToModify = resultItr.next(); T clonedModifiedObject = (T) Util.deepCopy(objectToModify); for (Entry<String, Object> entry : update.getUpdateData().entrySet()) { Object newValue = Util.deepCopy(entry.getValue()); if (encrypted && cmd.hasSecret() && cmd.isSecretField(entry.getKey())) { newValue = dbConfig.getCipher().encrypt(newValue.toString()); } try { BeanUtils.copyProperty(clonedModifiedObject, entry.getKey(), newValue); } catch (IllegalAccessException | InvocationTargetException e) { logger.error( "Failed to copy updated data into existing collection document using BeanUtils", e); return null; } } Object id = Util.getIdForEntity(clonedModifiedObject, cmd.getIdAnnotatedFieldGetterMethod()); clonedModifiedObjects.put(id, clonedModifiedObject); } JsonWriter jw = null; try { jw = new JsonWriter(dbConfig, cmd, collectionName, fileObjectsRef.get().get(collectionName)); } catch (IOException ioe) { logger.error("Failed to obtain writer for " + collectionName, ioe); throw new JsonDBException("Failed to save " + collectionName, ioe); } boolean updateResult = jw.updateInJsonFile(collection, clonedModifiedObjects); if (updateResult) { collection.putAll(clonedModifiedObjects); //Clone it once more because we want to disconnect it from the in-memory objects before returning. List<T> returnObjects = new ArrayList<T>(); for (T obj : clonedModifiedObjects.values()) { //Clone it once more because we want to disconnect it from the in-memory objects before returning. T returnObj = (T) Util.deepCopy(obj); if (encrypted && cmd.hasSecret() && null != returnObj) { CryptoUtil.decryptFields(returnObj, cmd, dbConfig.getCipher()); } returnObjects.add(returnObj); } return returnObjects; } return null; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); throw new JsonDBException( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); } finally { cmd.getCollectionLock().writeLock().unlock(); } }
From source file:io.jsondb.JsonDBTemplate.java
@Override public <T> List<T> findAllAndRemove(String jxQuery, String collectionName) { CollectionMetaData cmd = cmdMap.get(collectionName); @SuppressWarnings("unchecked") Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName); if ((null == cmd) || (null == collection)) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first."); }//from w ww.j a va 2 s .c o m cmd.getCollectionLock().writeLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); @SuppressWarnings("unchecked") Iterator<T> resultItr = context.iterate(jxQuery); Set<Object> removeIds = new HashSet<Object>(); while (resultItr.hasNext()) { T objectToRemove = resultItr.next(); Object idToRemove = Util.getIdForEntity(objectToRemove, cmd.getIdAnnotatedFieldGetterMethod()); removeIds.add(idToRemove); } if (removeIds.size() < 1) { return null; } JsonWriter jw; try { jw = new JsonWriter(dbConfig, cmd, collectionName, fileObjectsRef.get().get(collectionName)); } catch (IOException ioe) { logger.error("Failed to obtain writer for " + collectionName, ioe); throw new JsonDBException("Failed to save " + collectionName, ioe); } boolean substractResult = jw.removeFromJsonFile(collection, removeIds); List<T> removedObjects = null; if (substractResult) { removedObjects = new ArrayList<T>(); for (Object id : removeIds) { // Don't need to clone it, this object no more exists in the collection removedObjects.add(collection.remove(id)); } } return removedObjects; } finally { cmd.getCollectionLock().writeLock().unlock(); } }
From source file:io.jsondb.JsonDBTemplate.java
@Override public <T> T findAndRemove(String jxQuery, String collectionName) { if (null == jxQuery) { throw new InvalidJsonDbApiUsageException("Query string cannot be null."); }/*w w w.j a v a2 s .co m*/ CollectionMetaData cmd = cmdMap.get(collectionName); @SuppressWarnings("unchecked") Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName); if ((null == cmd) || (null == collection)) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first."); } cmd.getCollectionLock().writeLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); @SuppressWarnings("unchecked") Iterator<T> resultItr = context.iterate(jxQuery); T objectToRemove = null; while (resultItr.hasNext()) { objectToRemove = resultItr.next(); break; // Use only the first element we find. } if (null != objectToRemove) { Object idToRemove = Util.getIdForEntity(objectToRemove, cmd.getIdAnnotatedFieldGetterMethod()); if (!collection.containsKey(idToRemove)) { //This will never happen since the object was located based of jxQuery throw new InvalidJsonDbApiUsageException(String .format("Objects with Id %s not found in collection %s", idToRemove, collectionName)); } JsonWriter jw; try { jw = new JsonWriter(dbConfig, cmd, collectionName, fileObjectsRef.get().get(collectionName)); } catch (IOException ioe) { logger.error("Failed to obtain writer for " + collectionName, ioe); throw new JsonDBException("Failed to save " + collectionName, ioe); } boolean substractResult = jw.removeFromJsonFile(collection, idToRemove); if (substractResult) { T objectRemoved = collection.remove(idToRemove); // Don't need to clone it, this object no more exists in the collection return objectRemoved; } else { logger.error("Unexpected, Failed to substract the object"); } } return null; //Either the jxQuery found nothing or actual FileIO failed to substract it. } finally { cmd.getCollectionLock().writeLock().unlock(); } }
From source file:jsondb.JsonDBTemplate.java
@SuppressWarnings("unchecked") @Override//from w w w . ja v a2 s. c om public <T> T findOne(String jxQuery, String collectionName) { CollectionMetaData collectionMeta = cmdMap.get(collectionName); if ((null == collectionMeta) || (!collectionsRef.get().containsKey(collectionName))) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first"); } collectionMeta.getCollectionLock().readLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); Iterator<T> resultItr = context.iterate(jxQuery); while (resultItr.hasNext()) { T document = resultItr.next(); Object obj = Util.deepCopy(document); if (encrypted && collectionMeta.hasSecret() && null != obj) { CryptoUtil.decryptFields(obj, collectionMeta, dbConfig.getCipher()); } return (T) obj; // Return the first element we find. } return null; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { Logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); throw new JsonDBException( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); } finally { collectionMeta.getCollectionLock().readLock().unlock(); } }
From source file:jsondb.JsonDBTemplate.java
@SuppressWarnings("unchecked") @Override/*from w ww . java 2 s . c o m*/ public <T> List<T> find(String jxQuery, String collectionName) { CollectionMetaData cmd = cmdMap.get(collectionName); Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName); if ((null == cmd) || (null == collection)) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first."); } cmd.getCollectionLock().readLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); Iterator<T> resultItr = context.iterate(jxQuery); List<T> newCollection = new ArrayList<T>(); while (resultItr.hasNext()) { T document = resultItr.next(); Object obj = Util.deepCopy(document); if (encrypted && cmd.hasSecret() && null != obj) { CryptoUtil.decryptFields(obj, cmd, dbConfig.getCipher()); } newCollection.add((T) obj); } return newCollection; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { Logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); throw new JsonDBException( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); } finally { cmd.getCollectionLock().readLock().unlock(); } }
From source file:jsondb.JsonDBTemplate.java
@SuppressWarnings("unchecked") @Override//ww w.j ava 2s . c o m public <T> T findAndModify(String jxQuery, Update update, String collectionName) { CollectionMetaData cmd = cmdMap.get(collectionName); Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName); if ((null == cmd) || (null == collection)) { throw new InvalidJsonDbApiUsageException( "Collection by name '" + collectionName + "' not found. Create collection first."); } cmd.getCollectionLock().writeLock().lock(); try { JXPathContext context = contextsRef.get().get(collectionName); Iterator<T> resultItr = context.iterate(jxQuery); T objectToModify = null; T clonedModifiedObject = null; while (resultItr.hasNext()) { objectToModify = resultItr.next(); break; // Use only the first element we find. } if (null != objectToModify) { //Clone it because we dont want to touch the in-memory object until we have really saved it clonedModifiedObject = (T) Util.deepCopy(objectToModify); for (Entry<String, Object> entry : update.getUpdateData().entrySet()) { Object newValue = Util.deepCopy(entry.getValue()); if (encrypted && cmd.hasSecret() && cmd.isSecretField(entry.getKey())) { newValue = dbConfig.getCipher().encrypt(newValue.toString()); } try { BeanUtils.copyProperty(clonedModifiedObject, entry.getKey(), newValue); } catch (IllegalAccessException | InvocationTargetException e) { Logger.error( "Failed to copy updated data into existing collection document using BeanUtils", e); return null; } } Object idToModify = Util.getIdForEntity(clonedModifiedObject, cmd.getIdAnnotatedFieldGetterMethod()); JsonWriter jw = null; try { jw = new JsonWriter(dbConfig, cmd, collectionName, fileObjectsRef.get().get(collectionName)); } catch (IOException ioe) { Logger.error("Failed to obtain writer for " + collectionName, ioe); throw new JsonDBException("Failed to save " + collectionName, ioe); } boolean updateResult = jw.updateInJsonFile(collection, idToModify, clonedModifiedObject); if (updateResult) { collection.put(idToModify, clonedModifiedObject); //Clone it once more because we want to disconnect it from the in-memory objects before returning. T returnObj = (T) Util.deepCopy(clonedModifiedObject); if (encrypted && cmd.hasSecret() && null != returnObj) { CryptoUtil.decryptFields(returnObj, cmd, dbConfig.getCipher()); } return returnObj; } } return null; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { Logger.error("Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); throw new JsonDBException( "Error when decrypting value for a @Secret annotated field for entity: " + collectionName, e); } finally { cmd.getCollectionLock().writeLock().unlock(); } }