krysalis-update/src/java/org/krysalis/depot/update/monitor StandardMonitor.java,NONE,1.1 MonitorEvent.java,NONE,1.1 Monitor.java,NONE,1.1 IMonitor.java,NONE,1.1 StatisticsMonitor.java,NONE,1.1 ArtifactEvent.java,NONE,1.1 FileEvent.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:39 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/monitor
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/monitor

Added Files:
	StandardMonitor.java MonitorEvent.java Monitor.java 
	IMonitor.java StatisticsMonitor.java ArtifactEvent.java 
	FileEvent.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: ArtifactEvent.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.monitor;

import org.krysalis.depot.update.ArtifactInstance;

/**
 * @author arb_jack
 */
public class ArtifactEvent extends MonitorEvent {

	public static final int UNSET = 0;
	public static final int LISTED = 1;
	public static final int RESULT = 2;

	private int m_type = UNSET;
	private ArtifactInstance m_artifact = null;

	public ArtifactEvent(int type, ArtifactInstance artifact) {
		m_type = type;
		m_artifact = artifact;
	}

	/**
	 * @return
	 */
	public ArtifactInstance getArtifactInstance() {
		return m_artifact;
	}

	/**
	 * @param artifact
	 */
	public void setArtifactInstance(ArtifactInstance artifact) {
		m_artifact = artifact;
	}

	/**
	 * @return
	 */
	public int getType() {
		return m_type;
	}

	/**
	 * @param i
	 */
	public void setType(int i) {
		m_type = i;
	}

	public boolean isListed() { return LISTED == m_type; }
	public boolean isResult() { return RESULT == m_type; }
}

--- NEW FILE: StatisticsMonitor.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.monitor;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;
import org.krysalis.depot.update.ArtifactInstance;

/**
 * @author anou_mana
 */
public class StatisticsMonitor implements IMonitor, Dumpable {

	private int m_failed = 0;
	private int m_ignored = 0;
	private int m_parsed = 0;

	private List m_failedObjects = null;
	private List m_ignoredObjects = null;
	private List m_parsedObjects = null;

	private List m_artifacts = null;
	private List m_misgrouped = null;
	private List m_namegrouped = null;

	public StatisticsMonitor() {
		initClass();
	}

	private void initClass() {
		m_failedObjects = new ArrayList();
		m_ignoredObjects = new ArrayList();
		m_parsedObjects = new ArrayList();

		m_artifacts = new ArrayList();
		m_misgrouped = new ArrayList();
		m_namegrouped = new ArrayList();
	}

	public void notify(MonitorEvent event) {
		if (event instanceof FileEvent) {
			FileEvent fileEvent = (FileEvent) event;

			switch (fileEvent.getType()) {
				case FileEvent.FAILED :
					{
						m_failed++;
						m_failedObjects.add(fileEvent.getThingie());
						break;
					}
				case FileEvent.IGNORED :
					{
						m_ignored++;
						m_ignoredObjects.add(fileEvent.getThingie());
						break;
					}
				case FileEvent.PARSED :
					{
						m_parsed++;
						m_parsedObjects.add(fileEvent.getThingie());
						break;
					}
			}
		} else if (event instanceof ArtifactEvent) {
			ArtifactEvent artifactEvent = (ArtifactEvent) event;

			//
			//			switch (artifactEvent.getType()) {
			//				case ArtifactEvent.LISTED :
			//					{
			ArtifactInstance artifact = artifactEvent.getArtifactInstance();

			m_artifacts.add(artifact);

			String groupName = artifact.getArtifact().getGroup().getName();
			String artifactName = artifact.getIdentifier().getId();

			if (!groupName.equals(artifactName)) {

				boolean contains = false;
				String guess = null;
				for (StringTokenizer tokens =
					new StringTokenizer(artifactName, "-");
					tokens.hasMoreTokens();
					) {
					String token = tokens.nextToken();

					if (null == guess)
						guess = token;
					else
						guess += '-' + token;

					if (guess.equals(artifactName))
						contains = true;
				}

				if (!contains) {
					guess = null;
					for (StringTokenizer tokens =
						new StringTokenizer(artifactName, "_");
						tokens.hasMoreTokens();
						) {
						String token = tokens.nextToken();

						if (null == guess)
							guess = token;
						else
							guess += '_' + token;

						if (guess.equals(artifactName))
							contains = true;
					}
				}

				if (!contains)
					m_misgrouped.add(artifact);
				else
					m_namegrouped.add(artifact);
			}
			//						break;
			//					}
			//			}
		}
	}

	/**
	 * @return
	 */
	public int getFailed() {
		return m_failed;
	}

	/**
	 * @return
	 */
	public int getIgnored() {
		return m_ignored;
	}

	/**
	 * @return
	 */
	public int getParsed() {
		return m_parsed;
	}

	public void dump(PrintWriter out, int depth, boolean verbose) {
		final String indent = DebugUtils.getIndent(depth);

		DebugUtils.printSeparator(out, depth);
		out.print(indent);
		out.print("Statistics : ");
		DebugUtils.printSeparator(out, depth);

		out.print(indent);
		out.print("Failed  : ");
		out.println(m_failed);

		out.print(indent);
		out.print("Ignored : ");
		out.println(m_ignored);

		out.print(indent);
		out.print("Parsed  : ");
		out.println(m_parsed);

		out.print(indent);
		out.print("Artifacts  : ");
		out.println(m_artifacts.size());

		out.print(indent);
		out.print("MisGrouped  : ");
		out.println(m_misgrouped.size());
		out.print(indent);

		out.print("NameGrouped  : ");
		out.println(m_namegrouped.size());

		if (verbose) {
			DebugUtils.printSeparator(out, depth);
			DebugUtils.dump(out, depth, "Artifacts", verbose, m_artifacts);

			DebugUtils.printSeparator(out, depth);
			DebugUtils.dump(out, depth, "Parsed", verbose, m_parsedObjects);
		}

		DebugUtils.printSeparator(out, depth);
		DebugUtils.dump(out, depth, "Failed", verbose, m_failedObjects);

		DebugUtils.printSeparator(out, depth);
		DebugUtils.dump(out, depth, "Ignored", verbose, m_ignoredObjects);

		DebugUtils.printSeparator(out, depth);
		DebugUtils.dump(out, depth, "Mis-grouped", verbose, m_misgrouped);
		
		if (verbose) {
			DebugUtils.printSeparator(out, depth);
			DebugUtils.dump(out, depth, "Complete", verbose, m_namegrouped);
		}
	}
	/**
	 * @return Returns the failedObjects.
	 */
	public List getFailedObjects() {
		return m_failedObjects;
	}
	/**
	 * @return Returns the ignoredObjects.
	 */
	public List getIgnoredObjects() {
		return m_ignoredObjects;
	}
	/**
	 * @return Returns the namegrouped.
	 */
	public List getNameGrouped() {
		return m_namegrouped;
	}
	/**
	 * @return Returns the parsedObjects.
	 */
	public List getParsedObjects() {
		return m_parsedObjects;
	}
}

--- NEW FILE: Monitor.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.monitor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.common.util.debug.DebugUtils;

/**
 * @author arb_jack
 */
public class Monitor {
	
	// File Interface
	public final static int FAILED	=	1;
	public final static int IGNORED	=	2;
	public final static int PARSED	=	3;

	// Monitors Table...
	private List m_monitors = null;;

	//	Chaining...
	private Monitor m_next = null;

	//	The root singleton instance
	private static Monitor l_monitor = null;

	class MonitorContext {
		//
		// The context
		//
		Monitor m_currentMonitor = null;
		Stack m_stack = null;

		MonitorContext(Monitor currentMonitor) {
			m_stack = new Stack();
			push(currentMonitor);
		}

		public void push(Monitor currentMonitor) {
			m_stack.push(currentMonitor);
			m_currentMonitor = (Monitor) m_stack.peek();
		}

		public void pop(Monitor currentMonitor) {
			Monitor currentTop = (Monitor) m_stack.pop();
			// Set current
			m_currentMonitor = (Monitor) m_stack.peek();
			if (currentTop != currentMonitor) {
				//:TODO: But what (safely?) ???
			}
		}
		public Monitor getCurrentMonitor() {
			return m_currentMonitor;
		}
	}

	//
	// One per thread...
	//
	private static ThreadLocal l_context = null;

	//:TODO: Store a Stack/Current Monitor object in a ThreadLocal
	// variable, so can have high level code push a "context" and
	// lower level code will respect it (transparently)
	// Push a Monitor which chains (next) the current Monitor, to
	// get nested context...

	static {
		l_monitor = new Monitor();

		l_context = new ThreadLocal() {
			protected synchronized Object initialValue() {
				return l_monitor.new MonitorContext(l_monitor);
			}
		};
	}

	public Monitor(Monitor next) {
		initClass();
		m_next = next;
	}

	public Monitor() {
		initClass();
		m_next = null;
	}

	private void initClass() {
		m_monitors = new ArrayList();
	}

	public static Monitor getMonitor() {
		return ((MonitorContext) l_context.get()).getCurrentMonitor();
	}

	//
	//	--------------------------------------------------------------------
	//	Context Interface
	//
	public static Monitor pushContext(Object monitor) {
		Monitor newMonitor = pushContext();
		// :TODO: Fix level, ask monitor what they want...
		newMonitor.addMonitor(monitor);
		return newMonitor;
	}

	public static Monitor pushContext() {
		MonitorContext context = ((MonitorContext) l_context.get());
		Monitor newMonitor = new Monitor(context.getCurrentMonitor());
		context.push(newMonitor);
		return newMonitor;
	}

	public static Monitor pushContext(Monitor newContext) {
		((MonitorContext) l_context.get()).push(newContext);
		return newContext;
	}

	public static void popContext(Monitor expectedMonitor) {
		((MonitorContext) l_context.get()).pop(expectedMonitor);
	}

	/**
 	* Add a monitor, and it is 'registered' for
 	* whatever interfaces it supports
 	* 
 	* @param monitor
 	*/
	public void addMonitor(Object monitor) {
		m_monitors.add(monitor);
	}

	public void removeMonitor(Object monitor) {
		m_monitors.remove(monitor);
	}
	

	/**
 	* Notify interfaces that are single parameter only 
 	* 
 	* @param monitors
 	* @param notificationType
 	* @param obj
 	*/
	public void notify(MonitorEvent event) {

		for (Iterator i = m_monitors.iterator(); i.hasNext();) {
			try {
				((IMonitor) i.next()).notify(event);
			}
			catch (Throwable t) {
				// Their problems are not ours...
				Logger.getLogger().debug("Dispatch failure : " , t);
			}
		}
		
		if (null != m_next)
			m_next.notify(event);
	}
	
	/**
	 * Notify interfaes that are multipe parameter.
	 * @param Monitors
	 * @param notificationType
	 * @param objs
	 */
	
	private void notify(List Monitors, int notificationType, Object objs[]) {

	for (Iterator i = m_monitors.iterator(); i.hasNext();) {
		try {
			IMonitor monitor = ((IMonitor) i.next());

			switch(notificationType) {
				
						
				//case IGNORED:
				//	monitor.ignored(objs);
				//	break;
			}
		}
		catch (Throwable t) {
			// Their problems are not ours...
		}
	}
}

	public static void monitorToLog() {
		DebugUtils.enableCommonsLogging();
		getMonitor().addMonitor(new StandardMonitor());
	}
}

--- NEW FILE: MonitorEvent.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.monitor;

/**
 * @author arb_jack
 */
public class MonitorEvent {

}

--- NEW FILE: IMonitor.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.monitor;

/**
 * @author arb_jack
 */
public interface IMonitor {
	void notify(MonitorEvent event);
}

--- NEW FILE: FileEvent.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.monitor;

/**
 * @author arb_jack
 */
public class FileEvent extends MonitorEvent {

	public final static int UNSET = 0;
	public final static int PARSED = 1;
	public final static int IGNORED = 2;
	public final static int FAILED = 3;

	private int m_type = FileEvent.UNSET;
	private Object m_thingie = null;

	public FileEvent(int type, Object thingie) {
		m_type = type;
		m_thingie = thingie;
	}
	/**
	 * @return
	 */
	public Object getThingie() {
		return m_thingie;
	}

	/**
	 * @return
	 */
	public int getType() {
		return m_type;
	}

	/**
	 * @param object
	 */
	public void setThingie(Object object) {
		m_thingie = object;
	}

	/**
	 * @param i
	 */
	public void setType(int i) {
		m_type = i;
	}

}

--- NEW FILE: StandardMonitor.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.monitor;

import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;

/**
 * @author anou_mana
 */
public class StandardMonitor implements IMonitor {

	public void notify(MonitorEvent event) {
		if (event instanceof FileEvent) {
			FileEvent fileEvent = (FileEvent) event;

			switch (fileEvent.getType()) {
				case FileEvent.FAILED :
					{
						Logger.getLogger().debug(
							Messages.getString(
								MessageConstants.FAILED,
								fileEvent.getThingie()));
						break;
					}
				case FileEvent.IGNORED :
					{
						Logger.getLogger().debug(
							Messages.getString(
								MessageConstants.IGNORED,
								fileEvent.getThingie()));
						break;
					}
				case FileEvent.PARSED :
					{
						Logger.getLogger().debug(
							Messages.getString(
								MessageConstants.PARSED,
								fileEvent.getThingie()));
						break;
					}
			}
		}
	}

}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/