/** Remove all entries from the cache. */ public void flush() { Collection keys = this.entryMap.getAllKeys(this.category); // Notify the entries of their removal Iterator iter = keys.iterator(); while (iter.hasNext()) { Serializable key = (Serializable) iter.next(); TimedEntry entry = (TimedEntry) this.entryMap.get(this.category, key); entry.destroy(); } }
/** Remove the entry associated with key and call destroy on the entry if found. */ public void remove(Object key) { Serializable skey = (Serializable) key; try { TimedEntry entry = (TimedEntry) this.entryMap.remove(this.category, skey); if (entry != null) { entry.destroy(); } } catch (Exception e) { log.error("Failed to remove entry", e); } }
protected void storeEntity (Object id, Object obj) throws Exception { try { // Store fields java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); java.io.ObjectOutputStream out = new org.jboss.ejb.plugins.CMPClusteredInMemoryPersistenceManager.CMPObjectOutputStream (baos); Field[] f = obj.getClass ().getFields (); for (int i = 0; i < f.length; i++) { out.writeObject (f[i].get (obj)); } out.close (); this.ds.set (this.DS_CATEGORY, id.toString (), baos.toByteArray (), false); } catch (Exception e) { throw new EJBException ("Store failed", e); } }
@SuppressWarnings("unchecked") protected synchronized void publishLocalInvalidationGroups() throws Exception { this.localGroups = this.invalMgr.getInvalidationGroups(); this.log.debug("Publishing locally available invalidation groups: " + this.localGroups); ArrayList<InvalidationGroup> content = new ArrayList<InvalidationGroup>(this.localGroups); ArrayList<String> result = new ArrayList<String>(content.size()); for (int i = 0; i < content.size(); i++) { String aGroup = content.get(i).getGroupName(); result.add(aGroup); } String nodeName = this.partition.getNodeName(); DistributedState ds = this.partition.getDistributedStateService(); if (result.size() > 0) { NodeInfo info = new NodeInfo(result, nodeName); ds.set(this.RPC_HANDLER_NAME, nodeName, info, true); } else { ds.remove(this.RPC_HANDLER_NAME, nodeName, true); } }
/** Get the raw TimedEntry for key without performing any expiration check. @return the TimedEntry value associated with key if one exists, null otherwise. */ public TimedEntry peekEntry(Object key) { Serializable skey = (Serializable) key; TimedEntry entry = (TimedEntry) this.entryMap.get(this.category, skey); return entry; }
/** Get the cache value for key if it has not expired. If the TimedEntry is expired its destroy method is called and then removed from the cache. @return the TimedEntry value or the original value if it was not an instance of TimedEntry if key is in the cache, null otherwise. */ public Object get(Object key) { Serializable skey = (Serializable) key; TimedEntry entry = (TimedEntry) this.entryMap.get(this.category, skey); if (entry == null) return null; if (entry.isCurrent(this.now) == false) { // Try to refresh the entry if (entry.refresh() == false) { // Failed, remove the entry and return null entry.destroy(); try { this.entryMap.remove(this.category, skey); } catch (Exception e) { log.debug("Failed to remove expired entry", e); } return null; } } Object value = entry.getValue(); return value; }
Collection<?> coll = ds.getAllKeys(this.RPC_HANDLER_NAME); if (coll == null) ds.remove(this.RPC_HANDLER_NAME, nodeEntry, true);
@Override public void stopService() { DistributedReplicantManager drm = this.partition.getDistributedReplicantManager(); DistributedState ds = this.partition.getDistributedStateService(); try { this.partition.unregisterRPCHandler(this.RPC_HANDLER_NAME, this); ds.unregisterDSListenerEx(this.RPC_HANDLER_NAME, this); drm.unregisterListener(this.RPC_HANDLER_NAME, this); drm.remove(this.RPC_HANDLER_NAME); this.invalidationSubscription.unregister(); ds.remove(this.RPC_HANDLER_NAME, this.partition.getNodeName(), true); // this.invalMgr = null; // partition = null; this.invalidationSubscription = null; this.RPC_HANDLER_NAME = null; this.localGroups = null; this.bridgedGroups = new Vector<String>(); } catch (Exception e) { this.log.info("Problem while shuting down invalidation cache bridge", e); } }
public void start() throws Exception { super.start (); String ejbName = this.getContainer ().getBeanMetaData ().getEjbName (); this.DS_CATEGORY = "CMPClusteredInMemoryPersistenceManager-" + ejbName; this.ds.registerDSListenerEx (this.DS_CATEGORY, this); }
public int size() { return this.entryMap.getAllKeys(this.category).size(); }
public void stop() { super.stop (); this.ds.unregisterDSListenerEx (this.DS_CATEGORY, this); }
protected void updatedBridgedInvalidationGroupsInfo() Collection<NodeInfo> bridgedByNode = (Collection<NodeInfo>) this.partition.getDistributedStateService().getAllValues(this.RPC_HANDLER_NAME);
/** Get the cache value for key. This method does not check to see if the entry has expired. @return the TimedEntry value or the original value if it was not an instancee of TimedEntry if key is in the cache, null otherwise. */ public Object peek(Object key) { Serializable skey = (Serializable) key; TimedEntry entry = (TimedEntry) this.entryMap.get(this.category, skey); Object value = null; if (entry != null) { value = entry.getValue(); } return value; }
@Override public void startService() throws Exception { if (this.partition == null) throw new IllegalStateException("HAPartition property must be set before starting InvalidationBridge service"); this.RPC_HANDLER_NAME = "DCacheBridge-" + this.bridgeName; DistributedReplicantManager drm = this.partition.getDistributedReplicantManager(); DistributedState ds = this.partition.getDistributedStateService(); drm.add(this.RPC_HANDLER_NAME, ""); drm.registerListener(this.RPC_HANDLER_NAME, this); ds.registerDSListenerEx(this.RPC_HANDLER_NAME, this); this.partition.registerRPCHandler(this.RPC_HANDLER_NAME, this); // we now publish the list of caches we have access to if (this.invalMgr == null) { throw new IllegalStateException("Failed to find an InvalidationManagerMBean, ensure one is injected"); } this.publishLocalInvalidationGroups(); this.updatedBridgedInvalidationGroupsInfo(); this.invalidationSubscription = this.invalMgr.registerBridgeListener(this); }
/** * This method is called when collections of entities are to be found. The * persistence manager must find out whether the wanted instances are * available in the persistence store, and if so it must return a * collection of primaryKeys. * * @param finderMethod the find method in the home interface that was * called * @param args any finder parameters * @param instance the instance to use for the finder call * @return an primary key collection representing the found * entities * * @throws java.rmi.RemoteException thrown if some system exception occurs * @throws javax.ejb.FinderException thrown if some heuristic problem occurs */ public Collection findEntities (Method finderMethod, Object[] args, EntityEnterpriseContext instance, GenericEntityObjectFactory factory) throws Exception { Collection results = Collections.EMPTY_LIST; if (finderMethod.getName ().equals ("findAll")) { Collection tmpColl = this.ds.getAllKeys (DS_CATEGORY); if (tmpColl != null) results = GenericEntityObjectFactory.UTIL.getEntityCollection(factory, tmpColl); } return results; }
/** * This method is called when single entities are to be found. The * persistence manager must find out whether the wanted instance is * available in the persistence store, if so it returns the primary key of * the object. * * @param finderMethod the find method in the home interface that was * called * @param args any finder parameters * @param instance the instance to use for the finder call * @return a primary key representing the found entity * * @throws java.rmi.RemoteException thrown if some system exception occurs * @throws javax.ejb.FinderException thrown if some heuristic problem occurs */ public Object findEntity (Method finderMethod, Object[] args, EntityEnterpriseContext instance, GenericEntityObjectFactory factory) throws Exception { if (finderMethod.getName ().equals ("findByPrimaryKey")) { if (this.ds.get (DS_CATEGORY, args[0].toString ()) == null) throw new javax.ejb.FinderException (args[0]+" does not exist"); return factory.getEntityEJBObject(args[0]); } else return null; }
/** * * * Convenience method for sharing state across a cluster partition. * Delegates to the DistributedStateService * * @param key key for the distributed object * @param value the distributed object * */ public void setDistributedState(String key, Serializable value) throws Exception { DistributedState ds = this.getHAPartition().getDistributedStateService(); ds.set(this.getHAServiceKey(), key, value); }
/** * This method is called when an entity shall be removed from the * underlying storage. The persistence manager must call ejbRemove on the * instance and then remove its state from the underlying storage. * * @param ctx the instance to remove * * @throws java.rmi.RemoteException thrown if some system exception occurs * @throws javax.ejb.RemoveException thrown if the instance could not be removed */ public void removeEntity (EntityEnterpriseContext ctx) throws javax.ejb.RemoveException { try { if (this.ds.remove (this.DS_CATEGORY, ctx.getId ().toString (), false) == null) throw new javax.ejb.RemoveException ("Could not remove bean:" + ctx.getId ()); } catch (Exception e) { throw new javax.ejb.RemoveException (e.toString ()); } }
byte[] content = (byte[])this.ds.get (this.DS_CATEGORY, ctx.getId ().toString ());