001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.txtimer;
023:
024: // $Id: TimedObjectId.java 57209 2006-09-26 12:21:57Z dimitris@jboss.org $
025:
026: import org.jboss.mx.util.ObjectNameFactory;
027:
028: import javax.management.ObjectName;
029: import java.io.Serializable;
030:
031: /**
032: * The combined TimedObjectId consists of a String that identifies
033: * the "class" of the TimedObject and optionally an instance primary key object.
034: *
035: * When the TimedObject is an EJB deployed on JBoss, the containerId is the JMX
036: * name of the component, and the instancePk is the entity's primary key.
037: * If the component is not an entity, the instancePk should be null.
038: *
039: * @author Thomas.Diesler@jboss.org
040: * @version $Revision: 57209 $
041: * @since 09-Apr-2004
042: */
043: public class TimedObjectId implements Serializable {
044: private ObjectName containerId;
045: private Object instancePk;
046: private int hashCode;
047:
048: /**
049: * Construct a combined TimedObjectId
050: *
051: * @param containerId The TimedObject identifier
052: * @param instancePk The TimedObject instance identifier, can be null
053: */
054: public TimedObjectId(ObjectName containerId, Object instancePk) {
055: if (containerId == null)
056: throw new IllegalArgumentException(
057: "containerId cannot be null");
058:
059: this .containerId = containerId;
060: this .instancePk = instancePk;
061: }
062:
063: /**
064: * Construct a TimedObjectId
065: *
066: * @param timedObjectId The TimedObject identifier
067: */
068: public TimedObjectId(ObjectName timedObjectId) {
069: this (timedObjectId, null);
070: }
071:
072: public ObjectName getContainerId() {
073: return containerId;
074: }
075:
076: public Object getInstancePk() {
077: return instancePk;
078: }
079:
080: /**
081: * Parse the timed object id from external form.
082: * "[id=contatinerId,pk=instancePk]"
083: */
084: public static TimedObjectId parse(String externalForm) {
085: if (externalForm.startsWith("[") == false
086: || externalForm.endsWith("]") == false)
087: throw new IllegalArgumentException(
088: "Square brackets expected arround: " + externalForm);
089:
090: // take first and last char off
091: String inStr = externalForm.substring(1,
092: externalForm.length() - 1);
093:
094: if (inStr.startsWith("target=") == false)
095: throw new IllegalArgumentException("Cannot parse: "
096: + externalForm);
097: String jmxStr = inStr.substring(7);
098:
099: String pkStr = null;
100: int pkIndex = jmxStr.indexOf(",pk=");
101: if (pkIndex > 0) {
102: pkStr = jmxStr.substring(pkIndex + 4);
103: jmxStr = jmxStr.substring(0, pkIndex);
104: }
105:
106: ObjectName contatinerId = ObjectNameFactory.create(jmxStr);
107: return new TimedObjectId(contatinerId, pkStr);
108: }
109:
110: /**
111: * Returns the external representation of the TimedObjectId.
112: * "[id=contatinerId,pk=instancePk]"
113: */
114: public String toExternalForm() {
115: String pkStr = (instancePk != null ? ",pk=" + instancePk : "");
116: return "[target=" + containerId + pkStr + "]";
117: }
118:
119: public int hashCode() {
120: if (hashCode == 0)
121: hashCode = toString().hashCode();
122: return hashCode;
123: }
124:
125: public boolean equals(Object obj) {
126: if (obj == this )
127: return true;
128: if (obj instanceof TimedObjectId) {
129: TimedObjectId other = (TimedObjectId) obj;
130: if (containerId.equals(other.containerId))
131: return (instancePk != null ? instancePk
132: .equals(other.instancePk)
133: : other.instancePk == null);
134: }
135: return false;
136: }
137:
138: public String toString() {
139: return toExternalForm();
140: }
141: }
|