KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > txtimer > TimedObjectId


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ejb.txtimer;
23
24 // $Id: TimedObjectId.java 37858 2005-11-05 17:06:17Z dimitris $
25

26 import org.jboss.mx.util.ObjectNameFactory;
27
28 import javax.management.ObjectName JavaDoc;
29 import java.io.Serializable JavaDoc;
30
31 /**
32  * The combined TimedObjectId consists of a String that identifies
33  * the "class" of the TimedObject and optionally an instance primary key object.
34  *
35  * When the TimedObject is an EJB deployed on JBoss, the containerId is the JMX
36  * name of the component, and the instancePk is the entity's primary key.
37  * If the component is not an entity, the instancePk should be null.
38  *
39  * @author Thomas.Diesler@jboss.org
40  * @version $Revision: 37858 $
41  * @since 09-Apr-2004
42  */

43 public class TimedObjectId implements Serializable JavaDoc
44 {
45    private ObjectName JavaDoc containerId;
46    private Object JavaDoc instancePk;
47    private int hashCode;
48
49    /**
50     * Construct a combined TimedObjectId
51     *
52     * @param containerId The TimedObject identifier
53     * @param instancePk The TimedObject instance identifier, can be null
54     */

55    public TimedObjectId(ObjectName JavaDoc containerId, Object JavaDoc instancePk)
56    {
57       if (containerId == null)
58          throw new IllegalArgumentException JavaDoc("containerId cannot be null");
59
60       this.containerId = containerId;
61       this.instancePk = instancePk;
62    }
63
64    /**
65     * Construct a TimedObjectId
66     *
67     * @param timedObjectId The TimedObject identifier
68     */

69    public TimedObjectId(ObjectName JavaDoc timedObjectId)
70    {
71       this(timedObjectId, null);
72    }
73
74    public ObjectName JavaDoc getContainerId()
75    {
76       return containerId;
77    }
78
79    public Object JavaDoc getInstancePk()
80    {
81       return instancePk;
82    }
83
84    /**
85     * Parse the timed object id from external form.
86     * "[id=contatinerId,pk=instancePk]"
87     */

88    public static TimedObjectId parse(String JavaDoc externalForm)
89    {
90       if (externalForm.startsWith("[") == false || externalForm.endsWith("]") == false)
91          throw new IllegalArgumentException JavaDoc("Square brackets expected arround: " + externalForm);
92
93       // take first and last char off
94
String JavaDoc inStr = externalForm.substring(1, externalForm.length() - 1);
95
96       if (inStr.startsWith("target=") == false)
97          throw new IllegalArgumentException JavaDoc("Cannot parse: " + externalForm);
98       String JavaDoc jmxStr = inStr.substring(7);
99
100       String JavaDoc pkStr = null;
101       int pkIndex = jmxStr.indexOf(",pk=");
102       if (pkIndex > 0)
103       {
104          pkStr = jmxStr.substring(pkIndex + 4);
105          jmxStr = jmxStr.substring(0, pkIndex);
106       }
107
108       ObjectName JavaDoc contatinerId = ObjectNameFactory.create(jmxStr);
109       return new TimedObjectId(contatinerId, pkStr);
110    }
111
112    /**
113     * Returns the external representation of the TimedObjectId.
114     * "[id=contatinerId,pk=instancePk]"
115     */

116    public String JavaDoc toExternalForm()
117    {
118       String JavaDoc pkStr = (instancePk != null ? ",pk=" + instancePk : "");
119       return "[target=" + containerId + pkStr + "]";
120    }
121
122    public int hashCode()
123    {
124       if (hashCode == 0)
125          hashCode = toString().hashCode();
126       return hashCode;
127    }
128
129    public boolean equals(Object JavaDoc obj)
130    {
131       if (obj == this) return true;
132       if (obj instanceof TimedObjectId)
133       {
134          TimedObjectId other = (TimedObjectId)obj;
135          if (containerId.equals(other.containerId))
136             return (instancePk != null ? instancePk.equals(other.instancePk) : other.instancePk == null);
137       }
138       return false;
139    }
140
141    public String JavaDoc toString()
142    {
143       return toExternalForm();
144    }
145 }
146
Popular Tags