/*
* Copyright 2002-2008 the original author or authors.
*
* 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.gwtoolbox.ioc.core.rebind.processor.common;
import java.util.Set;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.typeinfo.JClassType;
import org.gwtoolbox.commons.generator.rebind.EasyTreeLogger;
import org.gwtoolbox.ioc.core.client.annotation.Logging;
import org.gwtoolbox.ioc.core.client.annotation.LoggingLevel;
import org.gwtoolbox.ioc.core.rebind.ProcessorInitializationException;
import org.gwtoolbox.ioc.core.rebind.config.*;
import org.gwtoolbox.ioc.core.rebind.processor.AbstractComponentContainerProcessor;
/**
* @author Uri Boness
*/
public class LoggingProcessor extends AbstractComponentContainerProcessor<Logging> {
private LoggingLevel level = LoggingLevel.FINE;
public void doInit(EasyTreeLogger logger, Logging logging, JClassType containerType, GeneratorContext context)
throws ProcessorInitializationException {
level = logging.value();
if (logger.debugEnabled()) {
logger.debug("Log level: " + level.name());
}
}
public void doProcess(EasyTreeLogger logger, MutableComponentContainerOracle oracle, GeneratorContext context) throws Exception {
for (ComponentDefinition definition : oracle.getComponentDefinitions()) {
logComponentDefinition(logger, definition);
}
for (RootPanelBindingDefinition definition : oracle.getRootPanelBindingDefinitions()) {
logRootPanelBindingDefinition(logger, definition);
}
}
//============================================== Helper Methods ====================================================
protected void logComponentDefinition(EasyTreeLogger logger, ComponentDefinition definition) {
EasyTreeLogger rootLogger = logger.getRoot();
if (level == LoggingLevel.BASIC) {
rootLogger.info("Component '" + definition.getId() + "'");
return;
}
EasyTreeLogger newLogger = rootLogger.branchInfo("Component '" + definition.getId() + "'");
newLogger.info("Type: " + definition.getComponentType().getQualifiedSourceName());
newLogger.info("Lazy: " + definition.isLazy());
newLogger.info("Singleton: " + definition.isSingleton());
newLogger.info("Generated: " + definition.isGenerated());
String generatedType = definition.getGeneratedType() != null ? definition.getGeneratedType().getQualifiedSourceName() : "<none>";
newLogger.info("Generated Type: " + generatedType);
newLogger.info("Init Method: '" + definition.getInitMethodName() + "'");
newLogger.info("Dispose Method: '" + definition.getDisposeMethodName() + "'");
if (level == LoggingLevel.FINEST) {
if (SimpleComponentDefinition.class.isInstance(definition)) {
SimpleComponentDefinition simpleDef = (SimpleComponentDefinition) definition;
// printing the properties settings
if (simpleDef.getPropertySettings().isEmpty()) {
newLogger.info("Properties Settings: <no properties settigns>");
} else {
EasyTreeLogger propLogger = newLogger.branchInfo("Properties Settings:");
for (PropertySetting setting : simpleDef.getPropertySettings()) {
propLogger.info(setting.getPropertyName() + " = " + setting.getValue());
}
}
// printing the construtor settings
ConstructorInjectionDefinition ctorDef = simpleDef.getConstructorInjectionDefinition();
if (ctorDef == null) {
newLogger.info("Construtor Settings: <no constructor arguments settigns>");
} else {
EasyTreeLogger ctorLogger = newLogger.branchInfo("Constructor Arguments Settings:");
int i = 0;
for (ParameterSetting setting : ctorDef.getParameterSettings()) {
StringBuilder builder = new StringBuilder(i++)
.append(setting.getParameter().getType() != null ? "[" + setting.getParameter().getType().getQualifiedSourceName() + "]" : "")
.append(" -> ")
.append(setting.getValue());
ctorLogger.info(builder.toString());
}
}
}
if (JavaConfigComponentDefinition.class.isInstance(definition)) {
JavaConfigComponentDefinition javaConfigDef = (JavaConfigComponentDefinition) definition;
newLogger.info("JavaConfig Method: '" + javaConfigDef.getMethodName() + "'");
}
}
if (level == LoggingLevel.FINER || level == LoggingLevel.FINEST) {
EasyTreeLogger metaDataLogger = newLogger.branchInfo("Meta Data:");
metaDataLogger.info("Source: " + definition.getMetaData().getSource());
Set<String> propertiesNames = definition.getMetaData().getAllPropertyNames();
if (propertiesNames.isEmpty()) {
metaDataLogger.info("Properties: <no properties>");
} else {
metaDataLogger = metaDataLogger.branchInfo("Properties:");
for (String propertyName : propertiesNames) {
metaDataLogger.info(propertyName + " = '" + definition.getMetaData().getProperty(propertyName) + "'");
}
}
}
}
private void logRootPanelBindingDefinition(EasyTreeLogger logger, RootPanelBindingDefinition definition) {
if (level == LoggingLevel.FINE || level == LoggingLevel.FINER || level == LoggingLevel.FINEST) {
String rootPanelId = definition.getRootId();
if (rootPanelId == null || rootPanelId.length() == 0) {
logger.info("RootPanelBinding: Component '" + definition.getComponentId() + "' is bound to the body element");
} else {
logger.info("RootPanelBinding: Component '" + definition.getComponentId() + "' is bound to element with id '" + rootPanelId + "'");
}
}
}
}