001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.core.event;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.ivy.core.IvyContext;
024: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
025: import org.apache.ivy.core.module.id.ModuleId;
026: import org.apache.ivy.core.module.id.ModuleRevisionId;
027: import org.apache.ivy.util.StringUtils;
028:
029: /**
030: * The root of all ivy events Any ivy event knows which ivy instance triggered the event (the
031: * source) and also has a name and a map of attributes. The name of the event represents the event
032: * type, usually there is a one - one mapping between event names and IvyEvent subclass, even if
033: * this is not mandatory. Example: pre-resolve pre-resolve-dependency post-download The map of
034: * attributes is a Map from String keys to String values. It is especially useful to filter events,
035: * and to get some of their essential data in some context where access to Java types is not easy
036: * (in an ant build file, for example), Example: pre-resolve (organisation=foo, module=bar,
037: * revision=1.0, conf=default) post-download (organisation=foo, module=bar, revision=1.0,
038: * artifact=foo-test, type=jar, ext=jar)
039: */
040: public class IvyEvent {
041: private EventManager source;
042:
043: private String name;
044:
045: private Map attributes = new HashMap();
046:
047: protected IvyEvent(String name) {
048: this .source = IvyContext.getContext().getEventManager();
049: this .name = name;
050: }
051:
052: /**
053: * Should only be called during event object construction, since events should be immutable
054: *
055: * @param key
056: * @param value
057: */
058: protected void addAttribute(String key, String value) {
059: attributes.put(key, value);
060: }
061:
062: protected void addMDAttributes(ModuleDescriptor md) {
063: addMridAttributes(md.getResolvedModuleRevisionId());
064: }
065:
066: protected void addMridAttributes(ModuleRevisionId mrid) {
067: addModuleIdAttributes(mrid.getModuleId());
068: addAttribute("revision", mrid.getRevision());
069: addAttributes(mrid.getExtraAttributes());
070: }
071:
072: protected void addModuleIdAttributes(ModuleId moduleId) {
073: addAttribute("organisation", moduleId.getOrganisation());
074: addAttribute("module", moduleId.getName());
075: }
076:
077: protected void addConfsAttribute(String[] confs) {
078: addAttribute("conf", StringUtils.join(confs, ", "));
079: }
080:
081: protected void addAttributes(Map attributes) {
082: this .attributes.putAll(attributes);
083: }
084:
085: public EventManager getSource() {
086: return source;
087: }
088:
089: public String getName() {
090: return name;
091: }
092:
093: /**
094: * Returns the attributes of this event, as a Map(String->String)
095: *
096: * @return the attributes of this event, as a Map(String->String)
097: */
098: public Map getAttributes() {
099: return new HashMap(attributes);
100: }
101:
102: public String toString() {
103: return getName() + " " + getAttributes();
104: }
105:
106: public boolean equals(Object obj) {
107: if (!(obj instanceof IvyEvent)) {
108: return false;
109: }
110: IvyEvent e = (IvyEvent) obj;
111:
112: return getSource().equals(e.getSource())
113: && getName().equals(e.getName())
114: && attributes.equals(e.attributes);
115: }
116:
117: public int hashCode() {
118: //CheckStyle:MagicNumber| OFF
119: int hash = 37;
120: hash = 13 * hash + getSource().hashCode();
121: hash = 13 * hash + getName().hashCode();
122: hash = 13 * hash + attributes.hashCode();
123: //CheckStyle:MagicNumber| ON
124: return hash;
125: }
126: }
|