/*
 * 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.apache.jmeter.monitor.model;

import org.apache.jmeter.monitor.parser.Parser;
import org.apache.jmeter.monitor.parser.ParserImpl;
import org.apache.jmeter.samplers.SampleResult;

/**
 * ObjectFactory is a simple factory class which creates new instances of
 * objects. It also provides convienant method to parse XML status results.
 */
public class ObjectFactory {

	private static ObjectFactory FACTORY = null;

	private static Parser PARSER = null;

	/**
	 * 
	 */
	protected ObjectFactory() {
		super();
		PARSER = new MonitorParser(this);
	}

	public static ObjectFactory getInstance() {
		if (FACTORY == null) {
			FACTORY = new ObjectFactory();
		}
		return FACTORY;
	}

	public synchronized Status parseBytes(byte[] bytes) {
		return PARSER.parseBytes(bytes);
	}

	public Status parseString(String content) {
		return PARSER.parseString(content);
	}

	public Status parseSampleResult(SampleResult result) {
		return PARSER.parseSampleResult(result);
	}

	public Status createStatus() {
		return new StatusImpl();
	}

	public Connector createConnector() {
		return new ConnectorImpl();
	}

	public Jvm createJvm() {
		return new JvmImpl();
	}

	public Memory createMemory() {
		return new MemoryImpl();
	}

	public RequestInfo createRequestInfo() {
		return new RequestInfoImpl();
	}

	public ThreadInfo createThreadInfo() {
		return new ThreadInfoImpl();
	}

	public Worker createWorker() {
		return new WorkerImpl();
	}

	public Workers createWorkers() {
		return new WorkersImpl();
	}

	protected static class MonitorParser extends ParserImpl {
		public MonitorParser(ObjectFactory factory) {
			super(factory);
		}
	}

	/**
	 * Basic method for testing the class
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		if (args != null && args.length == 2) {
			String file = null;
			// int count = 1;
			if (args[0] != null) {
				file = args[0];
			}
			if (args[1] != null) {
				// count = Integer.parseInt(args[1]);
			}
			try {
				ObjectFactory of = ObjectFactory.getInstance();
				java.io.File infile = new java.io.File(file);
				java.io.FileInputStream fis = new java.io.FileInputStream(infile);
				java.io.InputStreamReader isr = new java.io.InputStreamReader(fis);
				StringBuffer buf = new StringBuffer();
				java.io.BufferedReader br = new java.io.BufferedReader(isr);
				String line = null;
				while ((line = br.readLine()) != null) {
					buf.append(line);
				}
				System.out.println("contents: ");
				System.out.println(buf.toString());
				System.out.println("----------------------");
				Status st = of.parseBytes(buf.toString().getBytes());
				if (st == null) {
					System.out.println("parse failed");
				} else {
					System.out.println("parse successful:");
					System.out.println(st.getJvm().getMemory().getFree());
					System.out.println(st.getJvm().getMemory().getTotal());
					System.out.println(st.getJvm().getMemory().getMax());
					System.out.println("connector size: " + st.getConnector().size());
					Connector conn = (Connector) st.getConnector().get(0);
					System.out.println("conn: " + conn.getThreadInfo().getMaxThreads());
				}
			} catch (java.io.FileNotFoundException e) {
				e.printStackTrace();
			} catch (java.io.IOException e) {
				e.printStackTrace();
			}
		} else {
		}
	}
}

@@ -13,16 +13,21 @@
 ght 2004
+,2006
  The Apa
@@ -51,16 +51,16 @@
 dation.%0A
-
  *%0A * Li
@@ -653,16 +653,59 @@
 model;%0A%0A
+// For unit tests, @see TestObjectFactory%0A%0A
 import o
@@ -1089,23 +1089,16 @@
 private 
-static 
 Parser P
@@ -2262,16 +2262,16 @@
 tory) %7B%0A
+
 %09%09%09super
@@ -2291,1647 +2291,7 @@
 %7D%0A%09%7D
-%0A%0A%09/**%0A%09 * Basic method for testing the class%0A%09 * %0A%09 * @param args%0A%09 */%0A%09public static void main(String%5B%5D args) %7B%0A%09%09if (args != null && args.length == 2) %7B%0A%09%09%09String file = null;%0A%09%09%09// int count = 1;%0A%09%09%09if (args%5B0%5D != null) %7B%0A%09%09%09%09file = args%5B0%5D;%0A%09%09%09%7D%0A%09%09%09if (args%5B1%5D != null) %7B%0A%09%09%09%09// count = Integer.parseInt(args%5B1%5D);%0A%09%09%09%7D%0A%09%09%09try %7B%0A%09%09%09%09ObjectFactory of = ObjectFactory.getInstance();%0A%09%09%09%09java.io.File infile = new java.io.File(file);%0A%09%09%09%09java.io.FileInputStream fis = new java.io.FileInputStream(infile);%0A%09%09%09%09java.io.InputStreamReader isr = new java.io.InputStreamReader(fis);%0A%09%09%09%09StringBuffer buf = new StringBuffer();%0A%09%09%09%09java.io.BufferedReader br = new java.io.BufferedReader(isr);%0A%09%09%09%09String line = null;%0A%09%09%09%09while ((line = br.readLine()) != null) %7B%0A%09%09%09%09%09buf.append(line);%0A%09%09%09%09%7D%0A%09%09%09%09System.out.println(%22contents: %22);%0A%09%09%09%09System.out.println(buf.toString());%0A%09%09%09%09System.out.println(%22----------------------%22);%0A%09%09%09%09Status st = of.parseBytes(buf.toString().getBytes());%0A%09%09%09%09if (st == null) %7B%0A%09%09%09%09%09System.out.println(%22parse failed%22);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09System.out.println(%22parse successful:%22);%0A%09%09%09%09%09System.out.println(st.getJvm().getMemory().getFree());%0A%09%09%09%09%09System.out.println(st.getJvm().getMemory().getTotal());%0A%09%09%09%09%09System.out.println(st.getJvm().getMemory().getMax());%0A%09%09%09%09%09System.out.println(%22connector size: %22 + st.getConnector().size());%0A%09%09%09%09%09Connector conn = (Connector) st.getConnector().get(0);%0A%09%09%09%09%09System.out.println(%22conn: %22 + conn.getThreadInfo().getMaxThreads());%0A%09%09%09%09%7D%0A%09%09%09%7D catch (java.io.FileNotFoundException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D catch (java.io.IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Make parser an instance variable