1 22 package org.jboss.ejb.txtimer; 23 24 26 import org.jboss.mx.util.ObjectNameFactory; 27 28 import javax.management.ObjectName ; 29 import java.io.Serializable ; 30 31 43 public class TimedObjectId implements Serializable 44 { 45 private ObjectName containerId; 46 private Object instancePk; 47 private int hashCode; 48 49 55 public TimedObjectId(ObjectName containerId, Object instancePk) 56 { 57 if (containerId == null) 58 throw new IllegalArgumentException ("containerId cannot be null"); 59 60 this.containerId = containerId; 61 this.instancePk = instancePk; 62 } 63 64 69 public TimedObjectId(ObjectName timedObjectId) 70 { 71 this(timedObjectId, null); 72 } 73 74 public ObjectName getContainerId() 75 { 76 return containerId; 77 } 78 79 public Object getInstancePk() 80 { 81 return instancePk; 82 } 83 84 88 public static TimedObjectId parse(String externalForm) 89 { 90 if (externalForm.startsWith("[") == false || externalForm.endsWith("]") == false) 91 throw new IllegalArgumentException ("Square brackets expected arround: " + externalForm); 92 93 String inStr = externalForm.substring(1, externalForm.length() - 1); 95 96 if (inStr.startsWith("target=") == false) 97 throw new IllegalArgumentException ("Cannot parse: " + externalForm); 98 String jmxStr = inStr.substring(7); 99 100 String 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 contatinerId = ObjectNameFactory.create(jmxStr); 109 return new TimedObjectId(contatinerId, pkStr); 110 } 111 112 116 public String toExternalForm() 117 { 118 String 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 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 toString() 142 { 143 return toExternalForm(); 144 } 145 } 146 | Popular Tags |