Initialize reading of the log4j xml in any of the core bundles. Ensure that this bundle doesnt get bounced often.Configure the values to include log4j xml and reload delay.The init method can look something similar to the one below.
public void initialize() {
try {
try {
String logConfig = "D://temp//poc_log_config.xml";
long configReloadDelay = (long) (1 * 30 * 1000);
if (logConfig != null && (new java.io.File(logConfig).exists())) {
if (logConfig.endsWith("xml")) {
org.apache.log4j.xml.DOMConfigurator.configureAndWatch(
logConfig, configReloadDelay);
} else {
org.apache.log4j.PropertyConfigurator
.configureAndWatch(logConfig, configReloadDelay);
}
} else {
org.apache.log4j.BasicConfigurator.configure();
}
} catch (Exception initErr) {
initErr.printStackTrace();
}
} catch (Exception err) {
}
}
The log4j xml will look as follows
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="PRODUCT1" class="com.test.anyappender">
<param name="File" value="D:\\temp\\product1\\product1log.log"/>
<param name="MaxFileSize" value="100000000"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} - %m%n"/>
</layout>
<param name="Threshold" value="DEBUG"/>
</appender>
<appender name="PRODUCT2" class="com.test.anyAppender">
<param name="File" value="D:\\temp\\product2\\product2.log"/>
<param name="MaxFileSize" value="100000000"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} - %m%n"/>
</layout>
<param name="Threshold" value="DEBUG"/>
</appender>
<root>
<level value="OFF" />
</root>
<logger name="test1">
<appender-ref ref="PRODUCT1"/>
<level value="INFO" />
</logger>
<logger name="test2">
<appender-ref ref="PRODUCT2"/>
<level value="DEBUG" />
</logger>
</log4j:configuration>
Now in any bundle code you can access any component logger as follows
static final Logger TEST_LOGGER = Logger.getLogger("test1");
use TEST_LOGGER.debug("any debug message");
By the above approach we can just have a single log4j bundle in the repository and not only avoid issues occuring from static variables but also can take full benefit of log4j API's.
Sunday, November 15, 2009
Configuring Component based logging in OSGi using Log4j
Extending OSGi telnet for custom commands
OSGi telnet command provided by equinox can be easily extended to implement custom commands.
First we need to write a class with which implements the custom commands.the methods should start with _ to be identified as osgi telnet commands.The getHelp command needs to be overwritten ,which will display the command when help is hit on the telnet console.
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
public class CustomCommands implements CommandProvider {
@Override
public String getHelp() {
return "***********CUSTOM COMMANDS*******************\n";
}
public void _testcommand(CommandInterpreter ci) {
ci.println("arg 1" + ci.nextArgument());
ci.println("arg 2" + ci.nextArgument());
}
}
Next step is to write a BundleActivator and register the command Provider service.
@Override
public void start(BundleContext ctx) throws Exception {
customCmd = new CustomCommands();
//created a instance of the implementation of our custom commands
ctx.registerService(CommandProvider.class.getName(),customCmd, null);
}
Dont forget to add the bundle activator class to the manifest. Manifest should include the following.
Import-Package: org.eclipse.osgi.framework.console;version="[1.0.0,1.0.0]",
org.osgi.framework;version="[1.4.0,1.4.0]"
Bundle-Activator: com.command.extender.CommandBundleActivator
JMX using Spring
JMX aims to create standard API's for managing and monitoring java applications.
Managing - changing values at runtime.
Monitoring - Reporting certain conditions.
Mbean -is a java bean with a management interface.
Mbean can expose attributes(through getters and setters) or any opertaions.Management interface can be defined statically or at runtime through interfaces.MBean Server acts as a broker between interacting beans or beans and remote clients.
Mbean server maintains a key to all registered means which is called as object name.The default protocol used is RMI , but can be changed to SOAP or Hessian.
Using raw JMX APi's is difficult , spring simplifies this.
Use the following configuration to create a MBeanServer. This will be required normally only if we are running standalone code.Else most of the containers like tomcat or spring dm provide the default MBeanServer which can be used to register the Mbeans.
<bean id="MBeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPosssible" value="true">
</bean>
Configuration to register a bean as MBean to MBeanServer.
<bean id="jmsSample" class="com.pg.MonitorService"></bean>
<bean class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<util:map>
<entry key="service:name=jmsSample" value-ref="jmsSample" />
</util:map>
</property>
By this all methods and attributes of this can be accesed using jconsole. Sometimes this may not be what we want .we would want to control the set of operations which are visible through JMX.
The Spring MbeanExporter allows the following stratergies
1. ObjectnamingStratergy - this will help in identifying the mbean in the jconsole.
- a. KeyNamingStratergy(Default) - key passed will become the name
- b. IdentityNamingStratergy -generates a object name dynamically
- c. MetaDataNamingStratergy - reads object name from source level meta-data (annotations)
2. MBeanInfoAssembler
When we want to control what we are exposing through JMX this is the better approach
- SimpleReflectiveMbeanInfoAssembler -Default -Exposes all public methods and properties
- MethodNameBasedMbeanInfoAssembler Methods can be configured through spring xmls which will be exposed through JMX.
- MetadataMBeanInfoAssembler what needs to be exposed is controlled using source level annotations.
- InterfaceBasedMBeanInfoAssembler
<bean class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<util:map>
<entry key="service:name=jmsSample" value-ref="jmsSample" />
</util:map>
</property>
<property name="assembler">
<bean
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<value>com.pg.IService</value>
</property>
</bean>
</property>
</bean>
Here the jmsSample bean need not implement teh managedInterface.
b Using interfaceMapping property
<bean class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<util:map>
<entry key="service:name=jmsSample" value-ref="jmsSample" />
</util:map>
</property>
<property name="assembler">
<bean
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="interfaceMappings">
<util:map>
<entry key="service:name=jmsSampleService" value="com.pg.IService"></entry>
</util:map>
</property>
</property>
</bean>
Thea bean jmsSample must implement the interface mentioned in interfaceMapping property
This API helps in monitoring application using JMX.
http://jamonapi.sourceforge.net/
Thursday, November 12, 2009
Spring Training -Day 6
http://www.infoq.com/presentations/colyer-keynote-springone2gx
Telnet console 2401 – ssh shell -2402 in DM2 .
2.0 release will have both osgi console and dm shell (osgi console will be disabled by default)
Dm Kernel – lightweight – no web profile.
No plans to support EJB
Bundles in usr directory is not exposing service because – not in active state – need to get some code to move it to active state.
Bundles in pick up move to active state by default.
Import – org.osgi.framework – will not work , as jar is in lib directory .Work Around is to copy paste it into repository/bundles/usr.
Spring dm automatically creates a property for service by name – bean name.
Also application context itself is exposed as service – can be verified with osgi console
Logback – for logging in dm 2.0
http://logback.qos.ch/
In JVM failure - can be achieved using ranking attribute
Load time weaving – more complex when many bundles are there , so hardcoded in DM server - just below boot Classloader (this is not possible with aspectj being a bundle)
Require Bundle(used for Split packages)
Require bundle – all exported packages are imported
Deployment pipeline – Like wars and non osgi headers still works under spring DM because –this is processed by DM server to convert it into osgi complaint bundle - End result is visible in work directory
When Dm server starts all bundles are not seen in osgi console – They move to resolved state , only when required(looking to find required imports) they move to resolved state.
Osgi reference has bean name attribute which can be used as default filter.
Bundles have version , services have ranking – Service ranking is part of OSGi spec
Custom osgi commands – implement Command Provider – equinox specific
2.0 modules – has attribute - sticky for service.
Spring integration
Enterprise Integration Pattern –implementers are SI and Camel
http://www.ibm.com/developerworks/library/os-ecl-osgiconsole/index.html
Spring Training -Day 5
Create new platform (may be equinox 3.5) – eclipse may be using 3.4
Eclipse takes ini file – which when bundles added to a folder adds it to the file –Eclipse can do it easily.
Uses conflict may be ClassCastExceptions or Linkage Exceptions
http://blog.springsource.com/2008/11/22/diagnosing-osgi-uses-conflicts/
Uses is whatever the bundle is resolved to .(Ex If struts has import;log4j and in any Export blabla;uses log4j – no issues because it will resolves to log4j in applications classpath just forcing us to use the same log4j.. When uses is with version becomes very critical)
Ideally 3rd party guys should not be ideally using uses –unless they want to restrict , but still relaxed using import in struts lib without version – so whatever 3rd party resolves to in our lib.
Import without version no – resolves to latest.
Class loader performance may increase -steps reduced for loading from the conventional model.
Osgi command – packages - searches for the package in all bundles.
BundleContext- used to install/uninstall bundles, deal with events and services programmatically
When multiple services have same ranking – will be solved based on service id – least one – the first one registered.
Extender bundle
Spring extender bundle creates bundle context for bundles which are in ACTIVE state . May be this is the reason bundles in usr lib cannot exposes services.(because they don’t move to active state)
Application context for bundles are created asynchronously when DM server starts up. It can be changed to get a sync behaviour.
When synchronous, it does not spawn a new thread for creating context and waits until context is created.(If any bundle is picked up before by the extender that may run in a separate thread)
Appcontext fails – it uninstalls the bundle – hence cannot verify errors from osgi console.
Service mandatory wait is asynchronous.
Extender Bundle – is like SpringContextLoadListener in web Apps
Extender bundles creates appContext for all bundles which are in ACTIVE state.
Accessing BundleContext – implements BundleContextAware
Either put appcontext file in META-INF / spring or use Spring-Context header in manifest file.
Mandatory Service
Timeout
Cardinality - 1:1 ,0:1, 0:n,1:n – renamed as availability attribute in Dm 2.0
osgi: set and osgi:list
Consumers can listen to bind-unbind events
Publishers can register to register and unregister events.
RFC 124 – Moving towards component and service than spring specific osgi:service and bean .
Spring Training - Day 4

Spring Security
http://blog.springsource.com/2009/01/02/spring-security-customization-part-1-customizing-userdetails-or-extending-grantedauthority/
http://blog.springsource.com/2009/01/02/spring-security-customization-part-2-adjusting-secured-session-in-real-time/
Principal –User,Device or system that performs action
Authentication –Establishing principals credentials are proper.(LDAP ,Database etc)
Authorization –Deciding if a principal is allowed to perform an action.
Secured Item – resource being secured.
Spring security is Portable across containers.
Secure item – method , web page(url) or flow(web flow)
Voters – 2 default- role based and authenticate based
3 types of AccessDecision manager (judges)
1. AffirmativeBased (anyone says yes –it’s a yes) -Default
2. Unanimous based – very conservative – one voter says no – it’s a no
3. Consensus based – based on majority says yes – majority is configurable – all these are configurable
Configuration in Web Application
<security:http> - configures default accessmgr, default filters, voters, security interceptor etc
Configures a servlet filter in web.xml
<security intercept-url pattern=”…” filters=”” acess=””/> can be configured
Spring security Taglibs since spring 2.0
Method Security(based on AOP)
Xml based - point cuts in xml with access role
Configuration based – have to register to get this enabled. , also has JSR specific annotations
Bad to put security constraints inside class – use xml approach over annotations – security is infrastructure
Advanced Security In web –
HttpSessionContextIntegrationFilter – stores context in session - if exists gets it and binds to thread local –way back binds it to session
LogoutFilter –clears security context on logoff
AuthenticationProcessingFilter - If not authenticated then send to login page
ExceptionTranslationFilter – converts exception into http responses and redirects
FilterSecurityInterceptor - authorises web requests based on url patterns
Any of these can be replaced and any can be added in the required order.
So filters for all these purposes are already placed.
Security context can be obtained anywhere in the code using – spring class SecurityContext.getCtx();
Obfuscating – is just setting bean – to avoid knowing technology
Spring Remoting
Consistency across all protocols and flipping from one protocol to another is simple.
Supported Protocols
RMI
HttpInvoker – is a protocol –over http
EJB
Hessian/Burlap(for sending XML over HTTP –Less predictable with complex types)
Hessian –uses binary xml
Burlap –uses textual xml
Spring on Server & Client –Use Http Invoker
Java Env but no Web Server –Use RMI
Interop with other languages Using HTTP – Hessian
Spring JMS
Message,Destination,Connection,Session,MessageProducer,MesageConsumer
Spring JMSTemplate
DynamicDestinationResolver
JMSTemplate – takes connectionFactory,MessageConvertor,DestinationResolver,
Spring JMX
MBean is a standard java bean with an additional management interface – attributes (with getters and setters) and Operations
Rmi is default protocol – can be changed
JVM runs a default MBean server , we can also override this by one given from spring
<context mbean –export>
Default –export everything – mostly a bundle will have only mbeans exposed
Export bean as MBean.
Strategies –
objectNamingStratergy- keyNamingStratergy(Default) , IdetitynamingStratergy(naming by objected from JVM),MetaDataNamingStratergy (annotations) – not recommended
One strategy can be overriden by the other.
MBeanInfoAssembler- by implementations of the MBeanInfoAssembler interface
To run with JMX add -Dcom.sun.management.jmxremote as VM param
OSGI
Single Version mean atleast that version
Resolves to the greatest -
Running in eclipse
Added vm param for clean
In manifest given space for the next line – because its reserved for headers
Import package , not mentioned – binds to the latest
Resolved phase- doesn’t mean classes are loaded
When classloader gets GC’ed - when objects are Gc'ed and all bundles importing from this bundle are refreshed(osgi refresh command)
Using Eclipse to simulate Equinox
Run configurations - > OSGi Configurations – new - Uncheck target platform - In filter search org.eclipse.osgi(any version ) – select it – apply – run
ss will show only equinox bundle.
Change run config as - -Dosgi.clean=true
Create new project – plugin – new plugin project - select run environment as equinox
Can run this new project under equinox with the created run configurations – Run as ...
Saturday, November 7, 2009
Spring Training -Day 3
Transaction Management
ACID-Atomic,Consistent,Isolated,Durable
Only way to handle transaction is hold on to the same connection –only way of doing it.
JTA – not good , scalability becomes difficult .
Ex: EBay – develop undo behaviour –compensatory action – 3rd part its the only option- for every method need a undo method.
Can use any datasource – no need to tieing it to spring jdbc datasource
Lots of transaction management – datatSourceTransactionMgr,HibernateTransactionMgr,Jps…,Jta..
Connection pool – use apache DBCP or c3p0
<tx:annotation driven> - registering for annotations
<bean id=”transactionManager” class=”…:> … datasource as property </>
transactionManager – should be the name – else have to explicitly mention in the <tx:annotation ……transaction manger=””.>
@Transactional at class level – all public method(because proxy sees only public method) will be transactional
Can have timeout – default is 60 secs
Datasource , transaction – better use xmls – infrastructure components (Aspects annotation are better)
Xml based approach - define transaction mgr, then define transaction interceptor- <tx:advice… > define pointcut
Tx:advice – is just used for overiding , not required
Xml approach advantage - can take these point cut definition from property files.
Set isolation levels – else though not committed , one thread can see the other
For recursive methods with @transaction – the same transaction context will be used.
Transaction Propagation – Propagation.REQUIRES_New – new one transaction context will be created per transaction.
So for iterative ones – if inner @Transaction is marked for required_new ,inner one will be committed irrespective of the other.
Can avoid rollback –with noRollBack option
Annotations
---------------
@Autowired –field level ,method level and constructor level
<context:annotation-config/>
@Autowired default by Type .-for all auto wired injections including and filed injections.
@Autowired
Test x; though x is name of any bean it will look for type Test.
@Autowired for field injection – Doesnt require setters.
Multiple types or no types will give error.
@Autowired not only for setter – for any method,field,constructor
Autowired for collections – List<Types> - all these types will be autowired into the list.
@Autowired(required=false) – does not fail when no type is found
@Qualifier – filtering when injecting by type – properties have to be mentioned in the bean
[default is name – when no name for bean is mentioned it is taken as id- That is why we say ctx.getBeanByName() and still use id it works]
@Resource –JSR 250 – injection by name – equivalent to ref in xml – not specific to Spring its in JSR 250
@Resource – also for JNDI references
@Component - alternative for writing bean definition in xml can be use with @Scope
@Component can also have bean name-else classname will be taken as bean name with first letter as small letter
<context:component scope> is required to identify components. – no need for annotation config as this is a superset of it.
<context:component scope> - can have filters – includes , excludes etc – without this Component will not work
Component scanner is above annotation processor. – so need to register annotation processor when using component scanner.
@value annotation will work with spring 3.0 – no support in spring 2.5
Both xml and annotation beans - if they have same name xml will override annotations , if the bean names are different 2 instances will be created.
Testing
-----------
Junit 4 – has annotations
Test runner - @Runwith (SpringJunit4ClassRunner.class)
@ContextConfiguration - with location of appContext.xml
Follow naming convention to avoid giving location - <testcasename>-context.xml
In case we need access to applicationCtx beans write private field and write @Autowired over it.
Name of bean if annotated with @Component –same as class name with first letter as small letter
With map it will put key as bean name when autowired.
@autowire with qualifier is very useful
Stubs and Mocks are different
Mock libraries - EasyMock,Jmock ,Mokito
Mockito: http://mockito.org/
Example with mockito – works by proxy- how to ensure method is called n number of times
During db tests – data inserted is automatically deleted , default can override if required.
SpringWeb
------------------
Web Layer should be very thin layer speaking to the backend service layer.
Spring Web Flow - Same url will be used , server maintains a state based on which flow will continue.
ContextLoadListener in web.xml - Root ApplicatonContext
WebApplicationContextLoader - register in servletContext – For WebApplicationContext
Using DispatcherServlet
2 contexts , RootApplicationContext and WebApplicationContext.
Web (DispatcherServlet) context can rootContext but not the other way round.
Spring MVC – annotation suited
WebApplicationContextUtils – static helper for accessing spring beans in web context.
Processing Flow -> Request -> Dispatcher Servlet -> Handler (Controller) -> Result View(send to DispatcherServlet) -> View
@Controller- equivalent to struts actions classes but can have multiple mapping for different url’s per method inside it.
@RequestMapping -for mapng urls
@RequestParam – mapping fields from HttpRequest.
Controller returns View name – default is path to jsp page
If controller returns null – then default view based on request url.
DispatcherServlet delegates to viewResolver to map returned view names to view implementations.
Default View resolver treats view name as Web –App relative path.
View Resolver – can take prefix and suffix as params.
If returned as string from handler – its taken as a view name
.jpg)