KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > lifecycle > LiveObject


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solution. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8
9 /*
10  * Created on Oct 13, 2005
11  *
12  * Author Michelle Lei
13  * ZBS
14  */

15 package jfun.yan.lifecycle;
16
17 import java.util.Collections JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import jfun.util.WeakRef;
21
22 /**
23  * <p>
24  * This class encapsulates life-cycle information for a regular Java object.
25  * </p>
26  * Zephyr Business Solution
27  *
28  * @author Michelle Lei
29  *
30  */

31 public class LiveObject extends WeakRef {
32   private volatile Map JavaDoc procs;
33   /**
34    * To create a LiveObject for a regular java object.
35    * @param obj the regular java object.
36    * @param procs the map from phase key to {@link Procedure} objects.
37    */

38   public LiveObject(Object JavaDoc obj, Map JavaDoc procs) {
39     super(obj);
40     this.procs = procs;
41   }
42   /**
43    * To determine if a certain phase exists in the life-cycle.
44    * @param key the key of the phase.
45    * @return true if existent.
46    */

47   public boolean containsPhase(Object JavaDoc key){
48     return procs.containsKey(key);
49   }
50   /**
51    * Get the {@link Procedure} for a certain phase.
52    * @param key the key of the phase.
53    * @return the Procedure object for the phase. null if not found.
54    */

55   public Procedure getProc(Object JavaDoc key){
56     return (Procedure)procs.get(key);
57   }
58   /**
59    * To invoke the procedure for a certain phase.
60    * @param key the key of the phase.
61    * @param args the arguments to the procedure.
62    * @throws Throwable any exception thrown out of the procedure.
63    */

64   public void invoke(Object JavaDoc key, Object JavaDoc[] args)
65   throws Throwable JavaDoc{
66     final Object JavaDoc obj = super.get();
67     if(obj == null){
68       //garbage collected.
69
procs = Collections.EMPTY_MAP;
70       return;
71     }
72     final Procedure inv = getProc(key);
73     if(inv==null){
74       throw new IllegalArgumentException JavaDoc("unsupported key: "+key);
75     }
76     inv.invoke(obj, args);
77   }
78 }
79
Popular Tags