001: package com.ibm.emb.meb.listener;
002:
003: import java.net.URL;
004: import java.util.Arrays;
005:
006: import javax.emb.ListenerVetoException;
007: import javax.emb.MediaEntityLocal;
008: import javax.emb.MediaException;
009: import javax.emb.MediaListener;
010:
011: /**
012: * This generic implementation of the MediaListener interface is only used for
013: * testing purposes.
014: */
015: public final class GenericMediaListener implements MediaListener {
016:
017: private static final long serialVersionUID = 3256726177979838768L;
018:
019: private String listenerName;
020:
021: private byte[] content = null;
022:
023: private URL location = null;
024:
025: private String mimeType = null;
026:
027: private String description = null;
028:
029: private String name = null;
030:
031: /**
032: * Constructor for GenericMediaListener.
033: */
034: public GenericMediaListener() {
035: super ();
036: }
037:
038: public GenericMediaListener(String listenerName) {
039: super ();
040: this .listenerName = listenerName;
041: }
042:
043: public void handleAboutToChangeMediaEntity(MediaEntityLocal meb,
044: String propertyName) throws MediaException {
045: if (propertyName == null) {
046: throw new NullPointerException("property name was null");
047: }
048: if (meb == null) {
049: throw new NullPointerException("meb was null");
050: }
051: propertyName = propertyName.toLowerCase().trim();
052: if (propertyName.equals("content")) {
053: content = meb.getContent();
054: System.out.println("GMListener: content size: "
055: + content.length);
056: } else if (propertyName.equals("location")) {
057: location = meb.getLocation();
058: System.out.println("GMListener: location : "
059: + location.toExternalForm());
060: } else if (propertyName.equals("mimetype")) {
061: mimeType = meb.getMimeType();
062: System.out.println("GMListener: mimetype : " + mimeType);
063: } else if (propertyName.equals("description")) {
064: description = meb.getDescription();
065: System.out.println("GMListener: description : "
066: + description);
067: } else if (propertyName.equals("name")) {
068: name = meb.getName();
069: System.out.println("GMListener: name : " + name);
070: } else {
071: throw new MediaException(propertyName
072: + ": property not found");
073: }
074: }
075:
076: public void handleMediaEntityChanged(MediaEntityLocal meb,
077: String propertyName) throws MediaException {
078: if (propertyName == null) {
079: throw new NullPointerException("property name was null");
080: }
081: if (meb == null) {
082: throw new NullPointerException("meb was null");
083: }
084: propertyName = propertyName.toLowerCase().trim();
085:
086: if (propertyName.equals("content")) {
087: if (!java.util.Arrays.equals(content, meb.getContent()))
088: throw new ListenerVetoException("media content changed");
089: else
090: throw new MediaException(
091: "handleAboutToChangeMediaEntity was not called before changing content");
092:
093: } else if (propertyName.equals("location")) {
094: if (location == null ? meb.getLocation() != null
095: : (!location.equals(meb.getLocation())))
096: throw new ListenerVetoException("location has changed");
097: else
098: throw new MediaException(
099: "handleAboutToChangeMediaEntity was not called before changing location");
100:
101: } else if (propertyName.equals("mimetype")) {
102: if (mimeType == null ? meb.getMimeType() != null
103: : (!mimeType.equals(meb.getMimeType())))
104: throw new ListenerVetoException("mimetype has changed");
105: else
106: throw new MediaException(
107: "handleAboutToChangeMediaEntity was not called before changing mimeType");
108:
109: } else if (propertyName.equals("description")) {
110: if (description == null ? meb.getDescription() != null
111: : (!description.equals(meb.getDescription())))
112: throw new ListenerVetoException(
113: "description has changed");
114: else
115: throw new MediaException(
116: "handleAboutToChangeMediaEntity was not called before changing description");
117:
118: } else if (propertyName.equals("name")) {
119: if (name == null ? meb.getName() != null : (!name
120: .equals(meb.getName())))
121: throw new ListenerVetoException("name has changed");
122: else {
123: throw new MediaException(
124: "handleAboutToChangeMediaEntity was not called before changing proxy");
125: }
126: } else {
127: throw new MediaException(propertyName
128: + ": property not found");
129: }
130: }
131:
132: public boolean equals(Object obj) {
133: if (obj instanceof GenericMediaListener) {
134: GenericMediaListener gml = (GenericMediaListener) obj;
135: if (listenerName == null ? gml.listenerName == null
136: : listenerName.equals(gml.listenerName)) {
137: // if (proxy == null? gml.proxy == null:
138: // proxy.equals(gml.proxy)){
139: if (name == null ? gml.name == null : name
140: .equals(gml.name)) {
141: if (content == null ? gml.content == null : Arrays
142: .equals(content, gml.content)) {
143: if (location == null ? gml.location == null
144: : location.equals(gml.location)) {
145: if (mimeType == null ? gml.mimeType == null
146: : mimeType.equals(gml.mimeType)) {
147: if (description == null ? gml.description == null
148: : description
149: .equals(gml.description)) {
150: return true;
151: }
152: }
153: }
154: }
155: }
156: // }
157: }
158: }
159:
160: return false;
161: }
162:
163: public int hashCode() {
164: int hash = 1;
165: hash = hash
166: * 31
167: + ((listenerName == null) ? 0 : listenerName.hashCode());
168: hash = hash * 31
169: + ((description == null) ? 0 : description.hashCode());
170: hash = hash * 31
171: + ((mimeType == null) ? 0 : mimeType.hashCode());
172: hash = hash * 31 + ((name == null) ? 0 : name.hashCode());
173: hash = hash * 31
174: + ((location == null) ? 0 : location.hashCode());
175: int bytehash = 0;
176: if (content != null) {
177: for (int i = 0; i < content.length; i++) {
178: bytehash += new Byte(content[i]).hashCode();
179: }
180: }
181: hash = hash * 31 + bytehash;
182: return hash;
183: }
184:
185: public void handleAboutToRemoveMediaEntity(
186: MediaEntityLocal mediaEntity) throws MediaException {
187: if (mediaEntity == null)
188: throw new NullPointerException("media entity is null");
189: }
190:
191: }
|