org.headlessintrace.client.DefaultFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of headlessInTraceClient Show documentation
Show all versions of headlessInTraceClient Show documentation
A headless java API that collects events from other JVMs. Events=method invocations. Initial code taken from http://mchr3k.github.io/org.intrace/. Intended for building diagnostic applications.
package org.headlessintrace.client;
import org.headlessintrace.client.connection.DefaultCallback;
import org.headlessintrace.client.connection.DefaultConnection;
import org.headlessintrace.client.connection.DefaultTraceEventWriterImpl;
import org.headlessintrace.client.connection.IConnection;
import org.headlessintrace.client.connection.IConnectionStateCallback;
import org.headlessintrace.client.model.DefaultTraceEventParser;
import org.headlessintrace.client.model.ITraceEvent;
import org.headlessintrace.client.model.ITraceEventParser;
import org.headlessintrace.client.model.DefaultTraceEvent;
import org.headlessintrace.client.request.DefaultRequest;
import org.headlessintrace.client.request.DefaultRequestSeparator;
import org.headlessintrace.client.request.IRequest;
import org.headlessintrace.client.request.IRequestSeparator;
import org.headlessintrace.jdbc.HsqldbProvider;
import org.headlessintrace.jdbc.IJdbcProvider;
/**
* This is my low-complexity approach to dependency injection.
* This class decides on default implementations.
* If, application wide, you want to override a default, you do this:
* 1) At application startup time, instantiate your own implementation of IFactory (probably extending DefaultFactory).
* 2) Pass an instance of your new factory into DefaultFactory.setFactory(), as detailed here:
IFactory myCustomFactory = new DefaultFactory() {
@Override
public ITraceEventParser getEventParser() {
return new MyCustomTraceEventParser();
}
};
DefaultFactory.setFactory(myCustomFactory);
* Then, subsequent calls to DefaultFactory.getFactory() will return myCustomFactory.
* USE CASE: the headless InTrace client relies on the text of an event to be formatted in a very specific way.
* Let's say that multiple text formats must be supported. If so, just create the necessary implementation of ITraceEventParser
* and configure the right one (as described above) at startup.
* @author erikostermueller
*
*/
public class DefaultFactory implements IFactory {
private static IFactory INSTANCE = null;
private HumanReadableMessages messages = new AmericanEnglishMessages();
@Override
public HumanReadableMessages getMessages() {
return messages;
}
@Override
public void setMessages(HumanReadableMessages val) {
messages = val;
}
private Config config = new DefaultConfig();
@Override
public Config getConfig() {
return config;
}
@Override
public void setConfig(Config val) {
config = val;
}
/**
* This method was designed to be called just one time at application startup.
* This could be done in a single static block of a single class, that perhaps
* grabbed the name of the Factory impl from a -D parameter or similar.
* If this method detects that its already been called, it throws a runtime exception.
* I did that so the developer will know whether the "worst case" has happened (ie, when multiple code locations are trying to set the factory).
* @param myFactory
*/
public static void setFactory(IFactory myFactory) {
if (INSTANCE==null) {
INSTANCE = myFactory;
} else {
throw new RuntimeException("Factory has already been set to value [" + INSTANCE.getClass().getName() + "]");
}
}
/**
*
*
* @return
*/
public static IFactory getFactory() {
if( INSTANCE==null) {
return new DefaultFactory();
}
return INSTANCE;
}
@Override
public ITraceEventParser getEventParser() {
return new DefaultTraceEventParser();
}
@Override
public ITraceWriter getTraceWriter() {
return new DefaultTraceEventWriterImpl();
}
@Override
public IConnection getDormantConnection() {
DefaultConnection d = new DefaultConnection();
System.out.println(" $$$ in factory #################################");
System.out.println(d.toString());
return new DefaultConnection();
}
@Override
public ITraceEvent getTraceEvent() {
return new DefaultTraceEvent();
}
@Override
public IRequestSeparator getRequestSeparator() {
return new DefaultRequestSeparator();
}
@Override
public IRequest getRequest() {
return new DefaultRequest();
}
@Override
public IConnectionStateCallback getCallback() {
return new DefaultCallback();
}
@Override
public IJdbcProvider getJdbcProvider() {
return new HsqldbProvider();
}
// public static void log(String msg) {
// FileWriter fw = null;
// try {
// File output = new File("/tmp/InTrace.txt");
// fw = new FileWriter(output,true);
// fw.append(msg);
//
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// if (fw!=null)
// try {
// fw.flush();
// fw.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
//
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy