private void init() { if (fileName == null) { throw new IllegalStateException("setLogfile() must be called before init()"); } try { printStream = new PrintStream(new FileOutputStream(fileName, P6SpyOptions.getActiveInstance().getAppend())); } catch (IOException e) { e.printStackTrace(System.err); } }
@Override public P6LoadableOptions getOptions(P6OptionsRepository optionsRepository) { return new P6SpyOptions(optionsRepository); }
@Override public void load(Map<String, String> options) { setLogMessageFormat(options.get(LOG_MESSAGE_FORMAT)); setLogfile(options.get(LOGFILE)); setAppend(options.get(APPEND)); setDateformat(options.get(DATEFORMAT)); setAppender(options.get(APPENDER)); setModulelist(options.get(MODULELIST)); setDriverlist(options.get(DRIVERLIST)); setStackTrace(options.get(STACKTRACE)); setStackTraceClass(options.get(STACKTRACECLASS)); setAutoflush(options.get(AUTOFLUSH)); setReloadProperties(options.get(RELOADPROPERTIES)); setReloadPropertiesInterval(options.get(RELOADPROPERTIESINTERVAL)); setJNDIContextFactory(options.get(JNDICONTEXTFACTORY)); setJNDIContextProviderURL(options.get(JNDICONTEXTPROVIDERURL)); setJNDIContextCustom(options.get(JNDICONTEXTCUSTOM)); setRealDataSource(options.get(REALDATASOURCE)); setRealDataSourceClass(options.get(REALDATASOURCECLASS)); setRealDataSourceProperties(options.get(REALDATASOURCEPROPERTIES)); setDatabaseDialectDateFormat(options.get(DATABASE_DIALECT_DATE_FORMAT)); setDatabaseDialectTimestampFormat(options.get(DATABASE_DIALECT_TIMESTAMP_FORMAT)); setDatabaseDialectBooleanFormat(options.get(DATABASE_DIALECT_BOOLEAN_FORMAT)); setCustomLogMessageFormat(options.get(CUSTOM_LOG_MESSAGE_FORMAT)); setJmx(options.get(JMX)); setJmxPrefix(options.get(JMX_PREFIX)); }
public static synchronized void initialize() { final P6ModuleManager moduleManager = P6ModuleManager.getInstance(); if (null == moduleManager) { // not initialized yet => can't proceed return; } final P6SpyOptions opts = moduleManager.getOptions(P6SpyOptions.class); logger = opts.getAppenderInstance(); if (logger != null) { if (logger instanceof FileLogger) { final String logfile = opts.getLogfile(); ((FileLogger) logger).setLogfile(logfile); } if (logger instanceof FormattedLogger) { final MessageFormattingStrategy strategy = opts.getLogMessageFormatInstance(); if (strategy != null) { ((FormattedLogger) logger).setStrategy(strategy); } } } }
public void registerMBeans(Collection<P6LoadableOptions> allOptions) throws MBeanRegistrationException, InstanceNotFoundException, MalformedObjectNameException, NotCompliantMBeanException { boolean jmx = true; String jmxPrefix = ""; for (P6LoadableOptions options : allOptions) { if (options instanceof P6SpyOptions) { jmx = ((P6SpyOptions) options).getJmx(); jmxPrefix = ((P6SpyOptions) options).getJmxPrefix(); break; } } if (!jmx) { return; } // unreg possible conflicting ones first unregisterAllMBeans(jmxPrefix); // reg all for (P6LoadableOptions options : allOptions) { try { registerMBean(options, jmxPrefix); } catch (InstanceAlreadyExistsException e) { // sounds like someone registered beans already (before we had a chance to do so) // so let's just make things consistent and re-register again registerMBeans(allOptions); } } }
private P6MBeansRegistry constructMBean(P6MBeansRegistry mBeansRegistry, final String jmxPrefix, final boolean appendProperty) throws Exception { // none to be reused registry if (null == mBeansRegistry) { mBeansRegistry = new P6MBeansRegistry(); } final P6OptionsRepository repo = new P6OptionsRepository(); final P6SpyOptions opts = new P6SpyOptions(repo); opts.load(opts.getDefaults()); opts.setAppend(appendProperty); opts.setJmxPrefix(jmxPrefix); repo.initCompleted(); mBeansRegistry.registerMBeans(new ArrayList<P6LoadableOptions>(Arrays.asList(opts))); checkMBeanAppendProperty(jmxPrefix, appendProperty); return mBeansRegistry; }
@Override public void load(Map<String, String> options) { super.load(options); optionsRepository.set(String.class, REMOTE_SERVICE_NAME, options.get(REMOTE_SERVICE_NAME)); optionsRepository.set(Boolean.class, INCLUDE_PARAMETER_VALUES, options.get(INCLUDE_PARAMETER_VALUES)); }
public static synchronized void initialize() { final P6ModuleManager moduleManager = P6ModuleManager.getInstance(); if (null == moduleManager) { // not initialized yet => can't proceed return; } final P6SpyOptions opts = moduleManager.getOptions(P6SpyOptions.class); logger = opts.getAppenderInstance(); if (logger != null) { if (logger instanceof FileLogger) { final String logfile = opts.getLogfile(); ((FileLogger) logger).setLogfile(logfile); } if (logger instanceof FormattedLogger) { final MessageFormattingStrategy strategy = opts.getLogMessageFormatInstance(); if (strategy != null) { ((FormattedLogger) logger).setStrategy(strategy); } } } }
public void registerMBeans(Collection<P6LoadableOptions> allOptions) throws MBeanRegistrationException, InstanceNotFoundException, MalformedObjectNameException, NotCompliantMBeanException { boolean jmx = true; String jmxPrefix = ""; for (P6LoadableOptions options : allOptions) { if (options instanceof P6SpyOptions) { jmx = ((P6SpyOptions) options).getJmx(); jmxPrefix = ((P6SpyOptions) options).getJmxPrefix(); break; } } if (!jmx) { return; } // unreg possible conflicting ones first unregisterAllMBeans(jmxPrefix); // reg all for (P6LoadableOptions options : allOptions) { try { registerMBean(options, jmxPrefix); } catch (InstanceAlreadyExistsException e) { // sounds like someone registered beans already (before we had a chance to do so) // so let's just make things consistent and re-register again registerMBeans(allOptions); } } }
@Override public void load(Map<String, String> options) { setLogMessageFormat(options.get(LOG_MESSAGE_FORMAT)); setLogfile(options.get(LOGFILE)); setAppend(options.get(APPEND)); setDateformat(options.get(DATEFORMAT)); setAppender(options.get(APPENDER)); setModulelist(options.get(MODULELIST)); setDriverlist(options.get(DRIVERLIST)); setStackTrace(options.get(STACKTRACE)); setStackTraceClass(options.get(STACKTRACECLASS)); setAutoflush(options.get(AUTOFLUSH)); setReloadProperties(options.get(RELOADPROPERTIES)); setReloadPropertiesInterval(options.get(RELOADPROPERTIESINTERVAL)); setJNDIContextFactory(options.get(JNDICONTEXTFACTORY)); setJNDIContextProviderURL(options.get(JNDICONTEXTPROVIDERURL)); setJNDIContextCustom(options.get(JNDICONTEXTCUSTOM)); setRealDataSource(options.get(REALDATASOURCE)); setRealDataSourceClass(options.get(REALDATASOURCECLASS)); setRealDataSourceProperties(options.get(REALDATASOURCEPROPERTIES)); setDatabaseDialectDateFormat(options.get(DATABASE_DIALECT_DATE_FORMAT)); setDatabaseDialectTimestampFormat(options.get(DATABASE_DIALECT_TIMESTAMP_FORMAT)); setDatabaseDialectBooleanFormat(options.get(DATABASE_DIALECT_BOOLEAN_FORMAT)); setCustomLogMessageFormat(options.get(CUSTOM_LOG_MESSAGE_FORMAT)); setJmx(options.get(JMX)); setJmxPrefix(options.get(JMX_PREFIX)); }
@Override public void optionChanged(String key, Object oldValue, Object newValue) { if (key.equals(P6SpyOptions.RELOADPROPERTIES)) { reschedule(Boolean.valueOf(newValue.toString()), P6SpyOptions.getActiveInstance().getReloadPropertiesInterval()); } else if (key.equals(P6SpyOptions.RELOADPROPERTIESINTERVAL)) { reschedule(P6SpyOptions.getActiveInstance().getReloadProperties(), (Long) newValue); } }
@Override public P6LoadableOptions getOptions(P6OptionsRepository optionsRepository) { return new P6SpyOptions(optionsRepository); }
public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql, final String url) { String customLogMessageFormat = P6SpyOptions.getActiveInstance().getCustomLogMessageFormat();
final String format = P6SpyOptions.getActiveInstance().getDateformat(); final String stringNow; if (format == null) { final boolean stackTrace = P6SpyOptions.getActiveInstance().getStackTrace(); if (stackTrace) { final String stackTraceClass = P6SpyOptions.getActiveInstance().getStackTraceClass(); Exception e = new Exception(); if (stackTraceClass != null) {
result = new SimpleDateFormat(P6SpyOptions.getActiveInstance().getDatabaseDialectTimestampFormat()).format(value); } else if (value instanceof Date) { result = new SimpleDateFormat(P6SpyOptions.getActiveInstance().getDatabaseDialectDateFormat()).format(value); } else if (value instanceof Boolean) { if ("numeric".equals(P6SpyOptions.getActiveInstance().getDatabaseDialectBooleanFormat())) { result = Boolean.FALSE.equals(value) ? "0" : "1"; } else {
private static void cleanUp() throws MBeanRegistrationException, InstanceNotFoundException, MalformedObjectNameException { if (instance == null) { return; } for (P6OptionsSource optionsSource : instance.optionsSources) { optionsSource.preDestroy(instance); } if (P6SpyOptions.getActiveInstance().getJmx() // // unregister mbeans (to prevent naming conflicts) && instance.mBeansRegistry != null) { instance.mBeansRegistry.unregisterAllMBeans(P6SpyOptions.getActiveInstance().getJmxPrefix()); } // clean table plz (we need to make sure that all the configured factories will be re-loaded) new DefaultJdbcEventListenerFactory().clearCache(); }
@Before public void setUp() { // make sure to have no modules explicitly loaded by default { System.clearProperty(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST); P6SpyOptions.getActiveInstance().reload(); } }
final P6SpyLoadableOptions options = P6SpyOptions.getActiveInstance();
@Test public void testSetPropertyDiscartedOnAutoReload() throws Exception { // precondition assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); // value modification P6SpyOptions.getActiveInstance().setStackTrace(true); assertTrue(P6SpyOptions.getActiveInstance().getStackTrace()); // no explicit props reload, just modify timestamp and wait till autoreload happens FileUtils.touch(new File(System.getProperty(SpyDotProperties.OPTIONS_FILE_PROPERTY))); Thread.sleep(2000); // jmx value modification discarted assertFalse(P6SpyOptions.getActiveInstance().getStackTrace()); }
@After public void after() { // reset formatting setting P6SpyOptions.getActiveInstance().setCustomLogMessageFormat(null); }