| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.s4.processor; |
| |
| import org.apache.s4.ft.SafeKeeper; |
| import org.apache.s4.persist.ConMapPersister; |
| import org.apache.s4.persist.Persister; |
| import org.apache.s4.util.clock.Clock; |
| |
| import java.util.List; |
| |
| import org.apache.log4j.Logger; |
| |
| public class PrototypeWrapper { |
| |
| private static Logger logger = Logger.getLogger(PrototypeWrapper.class); |
| private AbstractPE prototype; |
| Persister lookupTable; |
| |
| public String getId() { |
| return prototype.getId(); |
| } |
| |
| public PrototypeWrapper(AbstractPE prototype, Clock s4Clock) { |
| this.prototype = prototype; |
| lookupTable = new ConMapPersister(s4Clock); |
| // TODO lookup table with PEIds |
| System.out.println("Using ConMapPersister .."); |
| // this bit of reflection is not a performance issue because it is only |
| // invoked at configuration time |
| try { |
| ((ConMapPersister) lookupTable).setSelfClean(true); |
| ((ConMapPersister) lookupTable).init(); |
| // set the persister in prototype |
| prototype.setLookupTable(lookupTable); |
| prototype.setPrototypeWrapper(this); |
| } catch (Exception e) { |
| // this is not expected |
| Logger.getLogger("s4") |
| .error("Exception invoking setLookupTable on prototype", e); |
| } |
| } |
| |
| |
| /** |
| * Find PE corresponding to keyValue. If no such PE exists, then a new one |
| * is created by cloning the prototype and this is returned. As a |
| * side-effect, the last update time for the PE in the lookup table is |
| * modified. |
| * |
| * @param keyValue |
| * key value |
| * @return PE corresponding to keyValue. |
| */ |
| public AbstractPE getPE(String keyValue) { |
| AbstractPE pe = null; |
| try { |
| pe = (AbstractPE) lookupTable.get(keyValue); |
| if (pe == null) { |
| pe = (AbstractPE) prototype.clone(); |
| pe.initInstance(); |
| } |
| // update the last update time on the entry |
| lookupTable.set(keyValue, pe, prototype.getTtl()); |
| |
| } catch (Exception e) { |
| logger.error("exception when looking up pe for key:" + keyValue, e); |
| } |
| |
| return pe; |
| } |
| |
| /** |
| * Find PE corresponding to keyValue. If no such PE exists, then null is |
| * returned. Note: the last update time is not modified in the lookup table. |
| * |
| * @param keyValue |
| * key value |
| * @return PE corresponding to keyValue, if such a PE exists. Null |
| * otherwise. |
| */ |
| public AbstractPE lookupPE(String keyValue) { |
| AbstractPE pe = null; |
| |
| try { |
| pe = (AbstractPE) lookupTable.get(keyValue); |
| |
| } catch (Exception e) { |
| logger.error("exception when looking up pe for key:" + keyValue, e); |
| } |
| |
| return pe; |
| } |
| |
| public void expire(String keyValue) { |
| try { |
| lookupTable.set(keyValue, null, 0); |
| } catch (Exception e) { |
| logger.error("exception when removing pe for key:" + keyValue, e); |
| } |
| |
| } |
| |
| public int getPECount() { |
| return lookupTable.keySet().size(); |
| } |
| |
| public List<EventAdvice> advise() { |
| return prototype.advise(); |
| } |
| |
| } |