<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Tech Tips Blog</title>
	<atom:link href="http://thetechtips.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://thetechtips.wordpress.com</link>
	<description>Learning by sharing</description>
	<lastBuildDate>Fri, 18 Nov 2011 01:15:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='thetechtips.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Tech Tips Blog</title>
		<link>http://thetechtips.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://thetechtips.wordpress.com/osd.xml" title="The Tech Tips Blog" />
	<atom:link rel='hub' href='http://thetechtips.wordpress.com/?pushpress=hub'/>
		<item>
		<title>J2EE Design Pattern Notes</title>
		<link>http://thetechtips.wordpress.com/2009/12/02/j2ee-design-pattern-notes/</link>
		<comments>http://thetechtips.wordpress.com/2009/12/02/j2ee-design-pattern-notes/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 17:42:28 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[application controller]]></category>
		<category><![CDATA[application service]]></category>
		<category><![CDATA[business delegate]]></category>
		<category><![CDATA[business object]]></category>
		<category><![CDATA[composite entity]]></category>
		<category><![CDATA[composite view]]></category>
		<category><![CDATA[context object]]></category>
		<category><![CDATA[DAO]]></category>
		<category><![CDATA[data access object]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[dispatcher view]]></category>
		<category><![CDATA[Domain store]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[EJB 3]]></category>
		<category><![CDATA[EJB client]]></category>
		<category><![CDATA[front controller]]></category>
		<category><![CDATA[intercepting filter]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[j2ee design patterns]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[mvc architecture]]></category>
		<category><![CDATA[service activator]]></category>
		<category><![CDATA[service locator]]></category>
		<category><![CDATA[service to worker]]></category>
		<category><![CDATA[servlets]]></category>
		<category><![CDATA[Session Façade]]></category>
		<category><![CDATA[transfer object]]></category>
		<category><![CDATA[transfer object assembler]]></category>
		<category><![CDATA[value list handler]]></category>
		<category><![CDATA[view helper]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[web service broker]]></category>

		<guid isPermaLink="false">http://thetechtips.wordpress.com/?p=86</guid>
		<description><![CDATA[These are notes I made while preparing for SCEA 5. I would like to thank my friend Sajitha for helping in scribing these notes. 1.     J2EE Design Patterns 1.1.             Application Controller Centralize retrieval and invocation of request processing components such as commands and views Advantages Front controller acts as the centralized access point and controller [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=86&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>These are notes I made while preparing for SCEA 5. I would like to thank my friend Sajitha for helping in scribing these notes.<span id="more-86"></span></p>
<h2>1.     J2EE Design Patterns</h2>
<h2>1.1.             Application Controller</h2>
<p><strong>Centralize retrieval and invocation of request processing components such as commands and views</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Front controller acts as the centralized access point and controller for incoming requests, whereas application controller is a component for identifying and invoking commands / actions and dispatching to views.</li>
<li>Improves modularity – Action and view management code in its own class</li>
<li>Better reusability and extensibility</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>              Struts 1.x RequestProcessor ,  ActionServlet to some extent</p>
<h2>1.2.             Application Service</h2>
<p><strong>Provides a central location to implement business logic that encapsulates Business Object and services</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Centralized reusable business and workflow logic that acts upon multiple Business objects</li>
<li>Better reusability by encapsulating inter-business object operations</li>
<li>Avoids duplication of code</li>
<li>Simplifies façade implmentation</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Additional layer introduced in the business tier</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.3.             Business Delegate</h2>
<p><strong>Encapsulates access to a business service – Acts like client side business abstraction</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Reduces coupling, better maintainability as client does not directly depend on business service</li>
<li>Translates business service exceptions</li>
<li>Improves availability as it can implement automatic recovery [in case of service failure] without exposing error to client</li>
<li>Exposes simpler uniform interface to the service</li>
<li>Can improve performance by caching into for common service requests</li>
<li>Hides remoteness, lookup etc</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Introduces another layer</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.4.             Business Object</h2>
<p><strong>Separates business data and logic using an object model.  Business objects encapsulate and manage business data, behavior and persistence</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Object oriented approach to business model implementation</li>
<li>Centralized business behavior and state promotes reuse</li>
<li>Separates persistence logic from business logic</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Plain old Java object [POJO, i.e. not JPA or EJB] implementation can induce and are susceptible to stale data [when used in distributed multi tier applications]</li>
<li>Adds extra layer of indirection.  May not be suitable when
<ul>
<li>Business model and business logic are trivial</li>
<li>Data model is a sufficient representation of the business model and letting presentation components access data directly using DAO is simpler</li>
</ul>
</li>
<li>Can result in bloated objects when more and more use-case specific behavior is implemented in BO</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.5.             Composite Entity</h2>
<p><strong>Implements persistent Business objects using local Entity Beans and POJOs. Aggregates a set of related BOs into coarse grained Entity Bean implementations</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>BOs can be implemented as parent objects and dependent objects</li>
<li>A parent object is reusable, independently deployable and manages its relation to other objects [May contain dependent objects[DOs]]</li>
<li>A dependent object can be a simple self-contained object or may contain other dependent objects [DOs]</li>
<li>Increases maintainability as number of fine grained entity beans is reduced</li>
<li>In EJB 2.x or later, dependent objects can be implemented as local entity beans to take advantage of Container Managed Persistence [CMP] and Container Managed Relationships [CMR]</li>
<li>Improves network performance by coarse grain entities
<ul>
<li>Improves overall performance in EJB 1.1</li>
<li>In EJB 2.x implementing BO As composite entity with local entity beans has the same benefit. </li>
</ul>
</li>
<li>Facilitates composite Transfer Object creation</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Local entity beans could impact performance – POJOs are faster than local entities due to container services</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.6.             Composite View</h2>
<p><strong>View is composed of multiple atomic sub-view.  Each sub-view can be included dynamically into the whole and the layout of the page can be managed independent of the contents</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Improves modularity and reuse [e.g. header &amp; footer]</li>
<li>Adds role based or policy based control as composite view and can conditionally include sub-views</li>
<li>Managing changes to portions of a template is more easy</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Aggregating atomic sub-view may result in potential display errors which can become a maintainability issue</li>
<li>Runtime inclusion of sub-views may have minimal performance hit</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Tiles</p>
<h2>1.7.             Context Object</h2>
<p><strong>Encapsulate state in a protocol independent way to be shared throughout the application.</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Improves reusability and maintainability [application is not polluted with protocol specific data types]</li>
<li>Easy testing as context encapsulates protocol information</li>
<li>Reduces constraints on evolution of interfaces [ one context as parameter instead of numerous objects as parameters]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Reduces performance because state is transferred from one object to another</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span>      </strong>ServletContext used by Servlets.   Protocol specific into is in concrete subclasses like HTTPServletContext</p>
<h2>1.8.             Data Access Object</h2>
<p><strong>Used to abstract and encapsulate all access to persistence store.  DAO manages connection with datasource to obtain and store data.  </strong></p>
<ul>
<li><em>Stateless</em></li>
<li><em>Does not cache data</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Enables transparency for clients [to the location and implementation of persistent storage mechanism]</li>
<li>Provides Object oriented view and encapsulates database schema</li>
<li>Easier migration to different databases as only DAO needs to change</li>
<li>Organizes all data access code into a separate layer</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Adds extra layer</li>
<li>Needs class hierarchy design – Prefer factory method strategy over Abstract Factory strategy</li>
<li>Introduces complexity to enable object oriented design</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>     </p>
<h2>1.9.             Dispatcher View</h2>
<p><strong>When minimal / limited business processing required, use Dispatcher view with views as the initial access point being a request.  The minimal business processing is managed by view.</strong></p>
<ul>
<li><em>Differs from ‘Service to worked’ – Business processing is done <span style="text-decoration:underline;">after</span> control is transferred to the view</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Leverages frameworks and libraries like JSTL</li>
<li>Separates processing logic from view [to View Helper]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Poor separation of View from Model and Control logic</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>              A direct link to a static HTML page or JSP that displays existing presentation model [data already in session]</p>
<h2>1.10.        Domain Store</h2>
<p><strong>Used to transparently persist an object model. Unlike CMP or BMP [in EJB 2.0], where persistence code is in object model, Domain store’s persistence mechanism us separate from object model</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Separates Bean Management from persistence logic</li>
<li>Improves testability, Bean management can be tested without actually persisting</li>
<li>Improves understanding of 3<sup>rd</sup> party persistence framework</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Creating a custom persistence framework is complex</li>
<li>Creating custom persistence might be an overkill for a small object model</li>
<li>Multi layer object tree loading and storing requires optimization techniques</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      EJB3 – EntityManager, Hibernate – SessionManager</p>
<h2>1.11.        Front Controller</h2>
<p><strong>Centralized access point for presentation tier request handling</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Initial point of contact for handling all related requests</li>
<li>Improves manageability</li>
<li>Improves reusability [ common code moves to controller]</li>
<li>Improves role separation</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Struts 1.x  ActionServlet</p>
<h2>1.12.        Intercepting Filter</h2>
<p><strong>Intercept and manipulate a request and a response before and after the request is processed in a centralized way.</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Centralized common processing across requests</li>
<li>Pre and post processing components loosely coupled</li>
<li>Filter manager combines loosely coupled filters in a chain</li>
<li>Improves reusability</li>
<li>Declarative and flexible configuration</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Sharing into among filters may be costly as they are loosely coupled</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>              Servlet Filters</p>
<h2>1.13.        Service Activator</h2>
<p><strong>Used to receive asynchronous requests and invoke one or more business services.  It is implemented as a JMS listener</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Integrates JMS into enterprise application</li>
<li>Provides asynchronous processing for any business tier component</li>
<li>Enables standalone JMS listener [no container support needed]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>EJB 2.x or later can use MDBs instead</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>     </p>
<h2>1.14.        Service Locator</h2>
<p><strong>Implements and encapsulates service and component lookup. Hides the details of lookup mechanism and encapsulates related dependencies</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Abstracts complexity of lookup from the client</li>
<li>Uniform service access to clients</li>
<li>Facilitates adding new components (as client is not aware of EJBHome objects or JMS connection factories, new components can be added without impacting client</li>
<li>Can improve network performance by aggregating network calls required to lookup and create components</li>
<li>Can improve performance by caching[e.g. using InitialContext or Factory objects]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>     </p>
<h2>1.15.        Service to Worker</h2>
<p><strong>Centralize control and request handling to retrieve a presentation model before turning control over to the view. The view generates a dynamic response based on presentation model.  </strong></p>
<p><strong>This pattern involves multiple patterns.</strong></p>
<ul>
<li><em>Application Controller – Request Handling</em></li>
<li><em>Front Controller – Centralized control</em></li>
<li><em>View Helper – View creation</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Better modularity, reusability, maintainability</li>
<li>Better role separation</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong><strong></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span>      </strong>ActionServlet,    RequestProcessor, and Action</p>
<p>Retrieve / construct ActionForm and process business logic before forwarding to JSP.<strong></strong></p>
<h2>1.16.        Session Façade</h2>
<p><strong>Encapsulates business tier components by exposing a coarse-grained service to remote clients. </strong></p>
<p><strong>Benefits:</strong></p>
<ul>
<li><em>Manageability</em></li>
<li><em>Centralized logic</em></li>
<li><em>Flexibility</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Session Façade implemented as Session Bean interacts with business components such as Business objects and application service</li>
<li>Reduces coupling between tiers</li>
<li>Reduces complexity
<ul>
<li>Using application service reduces complexity of Session Façade</li>
<li>Using business delegate to access session façades reduces complexity of client</li>
</ul>
</li>
<li>Improves performance by reducing fine grained remote calls</li>
<li>Centralizes transaction control with the coarse grained method</li>
<li>Exposes fewer remote interfaces to client</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>     </p>
<h2>1.17.        Transfer Object</h2>
<p><strong>It is used to carry multiple data elements across a tier. Transfer object is pass-by-value between tiers [if they are remote]</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Reduces network traffic</li>
<li>Simplifies remote interface as it can use coarse grained getData() type methods</li>
<li>Can reduce code duplication by using Entity inheritance TOs</li>
<li>With JPA entities are POJOs with annotation, once detached from entity manager, they are like TO – However for better control, validation etc, it may still be useful to have TOs separate</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      ActionForm</p>
<h2>1.18.        Transfer Object Assembler</h2>
<p><strong>Used to build an application model as a composite Transfer Object.  It aggregates multiple Transfer Objects from various business components and services and returns it to the client</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Reduces coupling between clients and the application model</li>
<li>Improves network performance by reducing number of remote calls [i.e. helps in getting many Transfer Objects at once]</li>
<li>Potential to introduce stale data</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.19.        Value List Handler</h2>
<p><strong>Value list handler is used to search, cache [the result]. And allow client to tranverse and select items from the result</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Provides search and iteration functionality. Uses DAO to get results for a query from database</li>
<li>Efficient alternative to EJB finders</li>
<li>Caches search results</li>
<li>Better network performance – only a subset of results is sent to the client instead of the entire list</li>
<li>Allows deferring entity bean transaction</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Creating a large list of Transfer Objects can be expensive – Mitigate this by creating Transfer object for a limited number of records from the query result</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>     </p>
<h2>1.20.        View Helper</h2>
<p><strong>Separates view from its processing logic. Use views to encapsulate formatting and helpers to encapsulate view processing logic</strong></p>
<ul>
<li><em>Helpers are adapters between View and Model [ like in the Model-View-Controller]</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Better application partitioning, reuse and maintainability</li>
<li>Eases testing as processing logic is in helper</li>
<li>Helper usage mirrors scriptlets</li>
</ul>
<p><strong><span style="text-decoration:underline;">Examples</span></strong>    Tag classes [e.g. to handle if-else check]<br />
Action &amp; ActionForm classes in Struts</p>
<h2>1.21.        Web Service Broker</h2>
<p><strong>Used to expose one or more services using XML and web protocols.  A web service broker is a coarse-grained service exposed as a web service.  It co-ordinates interactions among one or more services, aggregates responses and may demarcate and compensate transactions</strong></p>
<ul>
<li><em> </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Introduces a layer between client and service</li>
<li>Existing remote session façade need to be refactor-ed to support local access for the broker</li>
<li>Network performance may be impacted due to web protocols</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Examples</span></strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/86/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=86&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/12/02/j2ee-design-pattern-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
		<item>
		<title>GoF Design Pattern notes</title>
		<link>http://thetechtips.wordpress.com/2009/12/02/gof-design-pattern-notes/</link>
		<comments>http://thetechtips.wordpress.com/2009/12/02/gof-design-pattern-notes/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 17:39:04 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[abstract factory]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[bridge]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[cchain of responsibility]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[composite]]></category>
		<category><![CDATA[decorator]]></category>
		<category><![CDATA[difference]]></category>
		<category><![CDATA[facade]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[factory method]]></category>
		<category><![CDATA[fly weight]]></category>
		<category><![CDATA[flyweight]]></category>
		<category><![CDATA[gang of four]]></category>
		<category><![CDATA[GoF]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[mediator]]></category>
		<category><![CDATA[memento]]></category>
		<category><![CDATA[observer]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[similarity]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[state]]></category>
		<category><![CDATA[stretegy]]></category>
		<category><![CDATA[template method]]></category>
		<category><![CDATA[visitor]]></category>

		<guid isPermaLink="false">http://thetechtips.wordpress.com/?p=80</guid>
		<description><![CDATA[These are notes I made while preparing for SCEA 5. I would like to thank my friend Sajitha for helping in scribing these notes. 1.     GOF DESIGN PATTERNS 1.1.             Abstract Factory Provide an interface for creating families of related or dependent objects with out specifying their concrete classes Advantages One level of abstraction higher than [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=80&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>These are notes I made while preparing for SCEA 5. I would like to thank my friend Sajitha for helping in scribing these notes.<span id="more-80"></span></p>
<h2>1.     GOF DESIGN PATTERNS</h2>
<h2>1.1.             Abstract Factory</h2>
<p><strong>Provide an interface for creating families of related or dependent objects with out specifying their concrete classes</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>One level of abstraction higher than factory pattern – It is a factory method that returns one of several factories</li>
<li>It encapsulates a group of individual factories having a common theme</li>
<li>Isolates concrete classes</li>
<li>Makes exchanging product families easy</li>
<li>Promotes consistency among products</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Supporting new kind of products is difficult</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span>      </strong>EJBHome interface that creates new EJBObjects<br />
javax.servlet.jsp.JSPFactory<strong> – </strong>Abstract class that defines factory methods available to JSP at runtime</p>
<h2>1.2.             Adaptor</h2>
<p><strong>Convert the interface of a class to another interface that the client expects</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Allows two or more incompatible objects to interact</li>
<li>Reuse of older functionality</li>
<li>Adapter class ‘adapts’ adaptee to target by committing a concrete adapter class
<ul>
<li>Adapter is a subclass of adaptee</li>
<li>Introduces only one object for adaption</li>
<li>It will not work when a class and all its subclasses have to be ‘adapted’</li>
</ul>
</li>
<li>Object adapter has an instance variable of type Adaptee
<ul>
<li>Single adapter works with all of adaptees sub-classes</li>
<li>Makes it harder to override adaptee behavior</li>
</ul>
</li>
<li>Adapter is usually used to avoid changing existing classes [not an up-front design choice when both systems do not exist] Unlike Bridge pattern – both ends of adapter already exist and it glues them together</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Vendor specific subclasses of an abstract DAO</p>
<h2>1.3.             Bridge</h2>
<p><strong>Decouple an abstraction from the implementation so that the two can vary independently</strong></p>
<ul>
<li><em>Abstraction                  =  Entity</em></li>
<li><em>Implementation            =  Behavior</em></li>
<li><em>Similar to Adapter – The Entity adapts the behavior to a different interface [Entity interface]</em></li>
<li><em>Differs from Adapter – It is a design choice [both ends do not exist for Adapter</em></li>
<li><em>Similar to Strategy and State in structure</em></li>
<li><em>Differs from Strategy and State because these two design patterns have a single hierarchy of classes, so the behavior varies but the entity does not.</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Two hierarchies of classes that vary independently</li>
<li>The abstraction [entity] has-a implementation [behavior]</li>
<li>Used rarely, often combines with AbstractFactory – [creating a family of products – Entity and Behavior]</li>
<li>Hides implementation details from client</li>
<li>Allows to assign and change behavior of entities at runtime</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Behavior interface will be wide if the entities are highly orthogonal [Many varieties of operation]</li>
<li>Delegation from Entities to Behaviors may degrade performance</li>
<li>If a new entity is not satisfied with the current behavior interface, then changing interface needs extensive work</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong><strong>     </strong>DAO Implements Bridge pattern to separate services<strong></strong></p>
<h2>1.4.             Builder</h2>
<p><strong>Separates the construction of a complex object from its representation so that the same construction process can create different representation </strong></p>
<ul>
<li><em>Special case of strategy applied to create object</em></li>
<li><em>Director has a fixed high-level procedure, but the actual construction process depends on what is being built.  This construction process is encapsulated in a Strategy object called the Builder.  E.g. Builder separates the push scheduler and data formatter for HTTP GET request</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Lets you vary a products internal representation</li>
<li>Isolates code for construction from representation</li>
<li>Finer control over construction process
<ul>
<li>Director constructs the object step by step</li>
<li>Each step is implemented by a concrete builder</li>
<li>Once all steps are done Director retrieves the result</li>
</ul>
</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.5.             Chain of Responsibility</h2>
<p><strong>Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.  Chain the receiving objects and pass the request along the chain until an object handles it</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Reduced coupling – Client need not know the receiver of request</li>
<li>Added flexibility in assigning responsibilities to objects</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Receipt of request is not guaranteed</li>
<li>Chain may get lengthy and introduce performance problem</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Commonly used for parsers</p>
<h2>1.6.             Command</h2>
<p><strong>Encapsulate a request as an object to parameterize clients with different requests, queue, or log requests and support undo operations</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Easy to add new commands</li>
<li>Can be assembled into composite commands</li>
<li>Decouples the invoking object from the object that performs an operation</li>
<li>Similar to Strategy – execute is the strategy method</li>
<li>Differs to Strategy –
<ul>
<li>command is a user triggered action</li>
<li>Various commands can be unrelated – e.g. Strategy represents a single business rule such as tax calculations.  All Strategies do the same thing in a different way e.g. State Tax calculation verses Federal Tax calculation</li>
<li> </li>
</ul>
</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.7.             Composite</h2>
<p><strong>Compose objects into a tree structure to represent whole-part hierarchies</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Lets clients treat individual objects and compositions of objects uniformly</li>
<li>Defines hierarchies of primitive objects and composite objects</li>
<li>Makes it easier to ass new kinds of components</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Leaf has meaningless methods like add()</li>
<li>Design could be made overly general, making it harder to restrict the components of a composite e.g. Make a composite have only certain components</li>
<li>Type system will not enforce those contraints</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Aggregate Entity Beans,   XML DOM Model,     Swing/AWT containers</p>
<h2>1.8.             Decorator</h2>
<p><strong>Attach additional responsibilities to an object dynamically</strong></p>
<ul>
<li><em>Proxy is a special case of decorator pattern.  Proxy is used with regard to security and networking and typically a class with a proxy cannot be accessed directly.</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Modifies the behavior of individual objects without having to create a derived class and do this at run time</li>
<li>More flexibility then static inheritance [ ‘has-a’ not ‘is-a’]</li>
<li>Avoids feature laden classes high up in hierarchy</li>
<li>Each feature is implemented in separate decorator class</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>A decorator and its component are not identical.  So cannot rely on object identity when using this pattern</li>
<li>It results in a lot of little objects composing the system</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>    The java.io streams library implementation uses decorators to add responsibility to the stream</p>
<h2>1.9.             Façade</h2>
<p><strong>Unified interface to a set of interfaces in a system.</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Reduces number of objects a client has to deal with</li>
<li>Promotes weak coupling and less compilation dependencies</li>
<li>Clients can still use subsystem classes [i.e. they do not have to go to the façade]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Session entity façade pattern in J2EE</p>
<h2>1.10.        Factory Method</h2>
<p><strong>Defines an interface to create an object but it lets the subclasses decide which class to instantiate</strong></p>
<ul>
<li><em>A public creation method typically of the class that the method belongs to </em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Gives subclasses a hook for returning subclass</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Have to subclass creator just to create a particular concrete product</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      EJBHome interface,   Connection factories for EIS</p>
<h2>1.11.        Flyweight</h2>
<p><strong>Use sharing to support large number of fine grain objects efficiently</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Key concept is distinct between intrinsic and extrinsic state – Intrinsic state is stored in the flyweight, Extrinsic state depends and varies with flyweight’s context and so cannot be shared
<ul>
<li>Client objects are responsible for passing extrinsic state to flyweight</li>
</ul>
</li>
<li>Minimizes memory occupation by sharing as much data as possible with other similar objects</li>
<li>Reduction in number of objects to handle</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li> </li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Pooled EJBs,    Pooled database connections</p>
<h2>1.12.        Interpreter</h2>
<p><strong>Given a language, defines a representation for its grammar along with an Interpreter that uses the representation to interpret sentences in the language</strong></p>
<ul>
<li><em>Special case of Composite is applied to Parsing</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantage</span></strong></p>
<ul>
<li>Implementing grammar is easy</li>
<li>Easy to change and Extend grammar</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Complex grammars are hard to maintain. At least one class per rule.</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>     </p>
<h2>1.13.        Iterator</h2>
<p><strong>Provide a way to access elements of aggregate object with out exposing its underlying representation.</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Supports traversals of objects in a collection</li>
<li>Variation in traversal achieved just by changing iterator</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example </span></strong></p>
<h2>1.14.        Mediator</h2>
<p><strong>An object that encapsulates how a set of objects interact</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Centralized control</li>
<li>Loose coupling by preventing objects from referring each other</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example </span></strong></p>
<h2>1.15.        Memento</h2>
<p><strong> Without violating encapsulation, capture and externalize an object’s internal state so that it can be restored to this state later</strong></p>
<ul>
<li><em>Related to Command pattern regarding undo pattern</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Preserves encapsulation boundaries</li>
<li>Simplifies the originator [Object does not have to cache its own state as it changes]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Expensive if large data resides in Memento’s state</li>
<li>It may be difficult in some languages to ensure only originator can access memento’s state</li>
<li>Hidden cost of caring [and deleting] members</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      dfs</p>
<h2>1.16.        Observer</h2>
<p><strong>Defines a one-to-many dependency between objects, so that when one object changes state, all the dependents are notified and updated automatically</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Abstract coupling between subject and observer</li>
<li>Support for broadcast communication</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Unexpected updates – Since observers are unaware of each other’s presence, they can be blind to the cost of changing the subject.  A seemingly innocuous operation on the subject may cause a cascade of updates to observers and their dependent object</li>
<li>Without additional protocol to help observers discover what changed, they may be forced to work hard to deduce what changed</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Swing/AWT Listeners and Observers</p>
<h2>1.17.        Prototype</h2>
<p><strong>Specify the kind of object to instantiate using a prototypical instantiate using a prototypical instance and create new objects by copying this prototype</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Add/remove concrete products just by registering a prototypical instance with a client</li>
<li>Define new kinds of objects by instantiating existing classes and registering the instances as prototypes of client objects</li>
<li>The client exhibits different behavior by delegating responsibility to the prototype</li>
<li>Reduce sub-classing – Factory pattern often produces hierarchy of creator classes that parallels the product class hierarchy The prototype pattern allows cloning a prototype instead of using a factory method to create a new object</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.18.        Proxy</h2>
<p><strong>Provide a surrogate or place holder for another object to control access to it.</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>A remote proxy provides a local representation for an object in different address space</li>
<li>A virtual proxy creates expensive objects on demand</li>
<li>A protection proxy controls access to the original object</li>
<li>A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed [e.g. counting number of references, loading persistent objects to memory on access, etc ]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>    EJB’s remote interface is a proxy for a bean</p>
<p>                   Proxy is used in RMI</p>
<h2>1.19.        Singleton</h2>
<p><strong>Control the number of instances of a class to a constant usually one and provide single global point of access.</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Avoid polluting namespace with global variables</li>
<li>More flexible than class variables or methods</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      Math class in Java</p>
<h2>1.20.        State</h2>
<p><strong>State pattern allows an object to alter its behavior when it’s internal state changes.  The object will appear to change its class.</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Localizes state specific behavior and partitions behavior for different states</li>
<li>Makes state transition explicit – All variables of state change together in a single call.</li>
<li>State objects can be shared like flyweights if states do not have instance variable [Type is a state]</li>
<li>State objects are often singletons [Gof page 313]</li>
<li><span style="text-decoration:underline;">Similar to Strategy [&amp; Command]</span> – Single class/interface with many subclasses.  The behavior is that of Strategy / Command design pattern</li>
<li><span style="text-decoration:underline;">Differs from Strategy [&amp; Command]</span> – Intend is different.  Behavior is varied by changing state of object and not by performing[or invoking] a single action.</li>
<li><span style="text-decoration:underline;">Similar to Bridge </span>– State is the implementation of behavior for abstract object</li>
<li><span style="text-decoration:underline;">Differs from Bridge </span>– Bridge separates abstraction from implementation and is not based on the state of the object. Bridge pattern can implement any behavior on any instance of the abstraction</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.21.        Strategy</h2>
<p><strong>Defines a family of algorithms, encapsulates each one and makes them interchangeable [Alternative to sub classing – Context ‘has-a’ instead of a ‘is-a’ behavior]</strong></p>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Easier to extend a model to incorporate new behavior</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Clients must be aware of an understand different strategies</li>
<li>Increased number of objects</li>
<li>Single interface for all strategies so simpler implementations may not need all the parameters given – Communication overhead.</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong></p>
<h2>1.22.        Template Method</h2>
<p><strong>Defines a skeleton of an algorithm in an operation deferring some steps to subclasses</strong></p>
<ul>
<li><em>An alternative to Strategy when there are invariant parts of the algorithm</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong></p>
<ul>
<li>Fundamental technique for code reuse</li>
<li>Inverted control structure [parent class calls subclass’s method]</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      The director in builder pattern may be implementing this pattern – only difference being Builders are not subclasses of Director</p>
<h2>1.23.         Visitor</h2>
<p><strong>It represents an operation to be performed on the elements of an object structure.  </strong></p>
<ul>
<li><em>Visitor lets us define a new operation without changing the classes of the elements on which it operates</em></li>
<li><em>Each new operation is a subclass of the abstract Visitor</em></li>
</ul>
<p><strong><span style="text-decoration:underline;">Advantages</span></strong><strong></strong></p>
<ul>
<li>Visitor is after-the-fact substitute for multiple inheritance</li>
<li>When an element is visited, it calls the visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state</li>
<li>Gathers related operations and separates unrelated ones. [Related operations in visitor and unrelated in their won visitor subclasses</li>
<li>Visitor can visit objects that do not have same parent class</li>
</ul>
<p><strong><span style="text-decoration:underline;">Disadvantages</span></strong></p>
<ul>
<li>Adding new concrete elements is hard [needs new visitXXX method visitor] Consider whether it is most likely to change the algorithm [visitor] applied over an object structure or the classes that make up the structure</li>
<li>Breaking encapsulation to allow visitor to access state</li>
</ul>
<p><strong><span style="text-decoration:underline;">Example</span></strong>      jaxb XJC implementation uses Visitor pattern<strong></strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=80&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/12/02/gof-design-pattern-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
		<item>
		<title>How to Secure Java Webservice with encryption and signature</title>
		<link>http://thetechtips.wordpress.com/2009/09/30/secure-java-webservice-with-encryption-and-signature/</link>
		<comments>http://thetechtips.wordpress.com/2009/09/30/secure-java-webservice-with-encryption-and-signature/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 21:41:08 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[Webservices]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[jax ws]]></category>
		<category><![CDATA[metro]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Signature]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[webservice]]></category>
		<category><![CDATA[WS Security]]></category>
		<category><![CDATA[WS Security Policy]]></category>
		<category><![CDATA[WSIT]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://thetechtips.wordpress.com/?p=49</guid>
		<description><![CDATA[Introduction In the last post I had a short description on webservice security. In this post, we build a secure webservice and create a client to access it. For doing so, the same environment as in the Simple Webservice example is used. We will use Metro&#8217;s WSIT features to enable secrity for our webservice. Creating the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=49&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;"><strong>Introduction</strong></p>
<p style="text-align:left;">In the <a href="http://thetechtips.wordpress.com/2009/09/17/web-service-security/">last post </a>I had a short description on webservice security. In this post, we build a secure webservice and create a client to access it. For doing so, the same environment as in the <a href="http://thetechtips.wordpress.com/2009/08/27/how-to-create-a-metro-jax-ws-portable-webservice/">Simple Webservice example</a> is used. We will use Metro&#8217;s WSIT features to enable secrity for our webservice.<span id="more-49"></span></p>
<p style="text-align:left;"><strong>Creating the webservice</strong></p>
<p style="text-align:left;">The steps for creating and deploying an unsecured webservice are described in <a href="http://thetechtips.wordpress.com/2009/08/27/how-to-create-a-metro-jax-ws-portable-webservice/">this post</a>. Follow these steps till Step 6 (we do not deploy the webservice yet). Now we need to make the webservice secure.</p>
<p style="text-align:left;"><strong>Generate Key Pair</strong></p>
<p style="text-align:left;">In order to perform encryption and signature, the webservice and the client need a public-private key pair. The public key is wrapped inside a X509 certificate. In this example we do not use mutual certificates. The private key (or identity key) is with the webservice only. The public key (X509 certificate) is distributed to clients. What is encrypted using the public certificate, can only be decrypted using the private key and vice-versa. In this example, the private key is part of a KeyStore configured on the webservice, and the public certificate is part of a TrustStore configured on the client side.</p>
<p style="text-align:left;">To create a key-pair for use with this example, create a directory to keep the KeyStore and TrustStore in (call it <code>&lt;KEY_DIR&gt;</code>). Browse to this dir on command prompt and run<br />
<code>keytool -genkey -alias serviceKey -keypass mykeypass -keystore wsKeyStore.jks -storepass changeit -keyalg RSA -dname "CN=Tech Tips, OU=Blog, O=The Tech tips, L=DFW, ST=TX, C=US"</code></p>
<p style="text-align:left;">This creates the keystore with the private key in it. Now export the public certificate:<br />
<code>keytool -export -alias serviceKey -keypass mykeypass -keystore wsKeyStore.jks -storepass changeit -file publicCert.cer</code></p>
<p style="text-align:left;">and import the public certificate into a truststore:<br />
<code>keytool -import -alias publicCert -keystore wsTrustStore.jks -storepass changeit -file publicCert.cer</code></p>
<p style="text-align:left;"><strong>Specify Security Requirements</strong></p>
<p style="text-align:left;">In order to convey their security constraints to clients, webservices can include their security specifications as part of the WSDL. This is done by making use of WS-Security Policy (WSSP) assertions. The clients who have access to the WSDL therefore know the security contract for accessing the webservice.</p>
<p style="text-align:left;">Instead of creating a custom way of specifying security requirements when creating a webservice, WSIT (part of metro stack) makes use of the already developed way (using WSSP assertions). In order to enable security for our webservice, all we need is a static WSDL-like definitions file which contains the desire WSSP assertions assigned to our endpoint. This file needs to have a special name &#8216;<code>wsit-&lt;fully qualified implementation class name&gt;.xml</code>&#8216;.</p>
<p style="text-align:left;">In MetroWebserviceSample eclipse project&#8217;s <code>WEB-INF</code> directory, create a file named &#8216;<code>wsit-test.metro.sample.TestService.xml</code>&#8216;. The contents of this file should be as follows:</p>
<p><pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2-hudson-182-RC1. --&gt;
&lt;definitions targetNamespace=&quot;http://sample.metro.test/&quot;
 xmlns:tns=&quot;http://sample.metro.test/&quot;
 xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
 xmlns=&quot;http://schemas.xmlsoap.org/wsdl/&quot;
 xmlns:soap=&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot;
 xmlns:wsu=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd&quot;
 xmlns:wsp=&quot;http://schemas.xmlsoap.org/ws/2004/09/policy&quot;
 xmlns:sp=&quot;http://schemas.xmlsoap.org/ws/2005/07/securitypolicy&quot;
 xmlns:wspp=&quot;http://java.sun.com/xml/ns/wsit/policy&quot;&gt;
 
   &lt;wsp:Policy wsu:Id=&quot;TestService_Security_Policy&quot;&gt;
  &lt;wsp:ExactlyOne&gt;
   &lt;wsp:All&gt;   
    &lt;sp:SymmetricBinding &gt;
     &lt;wsp:Policy&gt;
      &lt;sp:ProtectionToken&gt;
       &lt;wsp:Policy&gt;
        &lt;sp:X509Token sp:IncludeToken=&quot;http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/Never&quot;&gt;
         &lt;wsp:Policy&gt;
          &lt;sp:WssX509V3Token10/&gt;
          &lt;sp:RequireDerivedKeys/&gt;
          &lt;sp:RequireIssuerSerialReference/&gt;
         &lt;/wsp:Policy&gt;
        &lt;/sp:X509Token&gt;
       &lt;/wsp:Policy&gt;
      &lt;/sp:ProtectionToken&gt;
      
      &lt;sp:AlgorithmSuite&gt;      
       &lt;wsp:Policy&gt;
        &lt;sp:Basic128/&gt;
       &lt;/wsp:Policy&gt;       
      &lt;/sp:AlgorithmSuite&gt;
      
      &lt;sp:Layout&gt;      
       &lt;wsp:Policy&gt;
        &lt;sp:Strict/&gt;
       &lt;/wsp:Policy&gt;       
      &lt;/sp:Layout&gt; 
      &lt;sp:IncludeTimestamp/&gt;  
      &lt;sp:EncryptBeforeSigning/&gt;   
     &lt;/wsp:Policy&gt;
    &lt;/sp:SymmetricBinding&gt;
    &lt;sp:Wss11&gt;
            &lt;wsp:Policy&gt;
              &lt;sp:MustSupportRefIssuerSerial/&gt;
            &lt;/wsp:Policy&gt;
          &lt;/sp:Wss11&gt;
             &lt;sc:KeyStore wspp:visibility=&quot;private&quot;
              xmlns:wspp=&quot;http://java.sun.com/xml/ns/wsit/policy&quot;
              xmlns:sc=&quot;http://schemas.sun.com/2006/03/wss/server&quot;
              location=&quot;&lt;KEY_DIR&gt;\wsKeyStore.jks&quot;
              type=&quot;JKS&quot; keypass=&quot;mykeypass&quot;
              alias=&quot;serviceKey&quot;
              storepass=&quot;changeit&quot;&gt;
             &lt;/sc:KeyStore&gt;
   &lt;/wsp:All&gt;
  &lt;/wsp:ExactlyOne&gt;
 &lt;/wsp:Policy&gt;
 
 &lt;wsp:Policy wsu:Id=&quot;TestService_Encryption_policy&quot;&gt;
  &lt;wsp:ExactlyOne&gt;
   &lt;wsp:All&gt;    
    &lt;sp:EncryptedParts xmlns:sp=&quot;http://schemas.xmlsoap.org/ws/2005/07/securitypolicy&quot;&gt;
     &lt;sp:Body /&gt;
    &lt;/sp:EncryptedParts&gt;      
   &lt;/wsp:All&gt;
  &lt;/wsp:ExactlyOne&gt;
 &lt;/wsp:Policy&gt;
 &lt;wsp:Policy wsu:Id=&quot;TestService_Signature_policy&quot;&gt;
  &lt;wsp:ExactlyOne&gt;
   &lt;wsp:All&gt;
    &lt;sp:SignedParts xmlns:sp=&quot;http://schemas.xmlsoap.org/ws/2005/07/securitypolicy&quot;&gt;
     &lt;sp:Body /&gt;
    &lt;/sp:SignedParts&gt;
   &lt;/wsp:All&gt;
  &lt;/wsp:ExactlyOne&gt;
 &lt;/wsp:Policy&gt;
 
 
&lt;portType name=&quot;TestService&quot;&gt;
  &lt;/portType&gt;
 
  &lt;binding name=&quot;TestServicePortBinding&quot; type=&quot;tns:TestService&quot;&gt;
   &lt;wsp:PolicyReference URI=&quot;#TestService_Security_Policy&quot;&gt;&lt;/wsp:PolicyReference&gt;
   &lt;wsp:PolicyReference URI=&quot;#TestService_Encryption_policy&quot;&gt;&lt;/wsp:PolicyReference&gt;
   &lt;wsp:PolicyReference URI=&quot;#TestService_Signature_policy&quot;&gt;&lt;/wsp:PolicyReference&gt;
  &lt;/binding&gt;
 
  &lt;service name=&quot;TestServiceService&quot;&gt;
     &lt;!-- Uncomment this section if above configured security is to be enabled --&gt;
     &lt;!-- --&gt;
  
&lt;port name=&quot;TestServicePort&quot; binding=&quot;tns:TestServicePortBinding&quot;&gt;
   &lt;/port&gt;
  &lt;/service&gt;
&lt;/definitions&gt;
</pre></p>
<p style="text-align:left;">The above file has the minimum content required for this configuration (in other situations we can have the complete WSDL, plus the policy declarations and references). It enforces encryption of the SOAP body element followed by signing the encrypted body (<code>EncryptBeforeSigning</code>) for all messages (requests and responses). For the purpose of encryption and signing derived keys based on an X509 token are used. The location of the token is specified using Metro specific element.</p>
<p style="text-align:left;"><strong>Deploy the Webservice</strong></p>
<li style="text-align:left;">Right click the eclipse project MetroWebserviceSample and export to a .war  file (MetroWebserviceSample.war).</li>
<li style="text-align:left;">Copy MetroWebserviceSample.war to &lt;TOMCAT_HOME&gt;/webapps and start the Tomcat server.</li>
<li style="text-align:left;">The WSDL can be looked up at <a href="http://localhost:8080/MetroWebserviceSample/test?wsdl">http://localhost:8080/MetroWebserviceSample/test?wsdl</a>.</li>
<p style="text-align:left;">Note that the element describing the location of X509 token (keystore etc) is not visible in the online WSDL.</p>
<p style="text-align:left;"><strong>Create the Client</strong></p>
<p style="text-align:left;">In order to create a POJO client which does not run inside a container, the Metro libraries will be needed. As indicated in <a href="http://thetechtips.wordpress.com/2009/08/27/how-to-create-a-metro-jax-ws-portable-webservice/" target="_blank">this post</a>, download Metro and put the expand the contents (call this directory <code>&lt;METRO_HOME&gt;</code>).  For convinience the directory <code>&lt;METRO_HOME&gt;/bin</code> can be added to the environment variable <code>Path</code>, so that the tools <code>wsgen</code> and <code>wsimport</code> can be used without qualified location. For JDK 6 onwards, these tools are part of the jdk.</p>
<p style="text-align:left;">Create a directory for the client code <code>&lt;CLIENT_HOME&gt;</code>. This can as well be an eclipse Java project. However, the instructions here are for running the client without using eclipse.</p>
<p style="text-align:left;">While the webservice is running on Tomcat, on the command prompt run:<br />
<code>wsimport -d &lt;CLIENT_HOME&gt; -s &lt;CLIENT_HOME&gt; -p test.metro.sample.client http://localhost:8080/MetroWebserviceSample/test?wsdl</code><br />
This will create the classes needed by the client, under the package structure <code>test.metro.sample.client</code>. Note that the classes are already compiled, so we will not recompile them.</p>
<p style="text-align:left;">In the directory <code>&lt;CLIENT_HOME&gt;/test/metro</code>, create a directory named <code>client</code>. Create a file <code>TestServiceClient.java</code> in this directory (so the package for this class is <code>test.metro.client</code>). The contents of <code>TestServiceClient.java</code> should be:</p>
<p><pre class="brush: java;">package test.metro.client;

import test.metro.sample.client.TestService;
import test.metro.sample.client.TestServiceService;

public class TestServiceClient {

 private String defaultGreeter = &quot;TheTechTips&quot;;
 /**
  * @param args
  */
 public static void main(String[] args) {
  
  TestServiceClient client = new TestServiceClient();
  TestService service = new TestServiceService().getPort(TestService.class);
  System.out.println(&quot;Greeatings are...&quot;);
  if (args.length == 0) {
   System.out.println(service.greet(client.defaultGreeter));
  } else {
   for (String greeter : args) {
    System.out.println(service.greet(greeter));
   }
  }
 }

}

</pre></p>
<p style="text-align:left;">To compile the above class, browse to <code>&lt;CLIENT_HOME&gt;</code> directory on command prompt (for convinience) and run:<br />
<code>javac -cp %CLASSPATH%;.;&lt;METRO_HOME&gt;/lib/webservices-api.jar test/metro/client/TestServiceClient.java</code></p>
<p style="text-align:left;"><strong>Client Security Configuration</strong></p>
<p style="text-align:left;">Similar to the wsdl-like file we created for securing the webservice, we need to specify where the client can find the security certificate to send proper requests to the service. The client config file should be named <code>&lt;service&gt;Service.xml</code> and placed in the <code>META-INF</code> directory. In addition, another xml file called <code>wsit-client.xml</code> is to be pesent in <code>META-INF</code>. The latter file imports the former.</p>
<p style="text-align:left;">Create a <code>META-INF</code> directory under <code>&lt;CLIENT_HOME&gt;</code>. Put files <code>TestServiceService.xml</code> and <code>wsit-client.xml</code> in the <code>META-INF</code> directory with following contents.</p>
<p style="text-align:left;">TestServiceService.xml</p>
<p><pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2-hudson-182-RC1. --&gt;
&lt;definitions targetNamespace=&quot;http://sample.metro.test/&quot;
 xmlns:tns=&quot;http://sample.metro.test/&quot;
 xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
 xmlns=&quot;http://schemas.xmlsoap.org/wsdl/&quot;
 xmlns:soap=&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot;
 xmlns:wsu=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd&quot;
 xmlns:wsp=&quot;http://schemas.xmlsoap.org/ws/2004/09/policy&quot;
 xmlns:sp=&quot;http://schemas.xmlsoap.org/ws/2005/07/securitypolicy&quot;
 xmlns:wspp=&quot;http://java.sun.com/xml/ns/wsit/policy&quot;&gt;
 
 &lt;wsp:Policy xmlns:sc=&quot;http://schemas.sun.com/2006/03/wss/client&quot;
  xmlns:wspp=&quot;http://java.sun.com/xml/ns/wsit/policy&quot;
  wsu:Id=&quot;TestService_Security_Client_Policy&quot;&gt;
  &lt;wsp:ExactlyOne&gt;
   &lt;wsp:All&gt;
      &lt;sc:TrustStore wspp:visibility=&quot;private&quot; location=
        &quot;&lt;KEY_DIR&gt;\wsTrustStore.jks&quot;
        storepass=&quot;changeit&quot;
        peeralias=&quot;publicCert&quot;/&gt;

   &lt;/wsp:All&gt;
  &lt;/wsp:ExactlyOne&gt;
 &lt;/wsp:Policy&gt;

 
&lt;portType name=&quot;TestService&quot;&gt;
  &lt;/portType&gt;
 
  &lt;binding name=&quot;TestServicePortBinding&quot; type=&quot;tns:TestService&quot;&gt;
   &lt;wsp:PolicyReference URI=&quot;#TestService_Security_Client_Policy&quot;&gt;&lt;/wsp:PolicyReference&gt;
  &lt;/binding&gt;
 
  &lt;service name=&quot;TestServiceService&quot;&gt;
     &lt;!-- Uncomment this section if above configured security is to be enabled --&gt;
     &lt;!-- --&gt;
  
&lt;port name=&quot;TestServicePort&quot; binding=&quot;tns:TestServicePortBinding&quot;&gt;
   &lt;/port&gt;
  &lt;/service&gt;
&lt;/definitions&gt;
</pre></p>
<p style="text-align:left;">wsit-client.xml</p>
<p><pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
 &lt;definitions
 xmlns=&quot;http://schemas.xmlsoap.org/wsdl/&quot;
 xmlns:wsdl=&quot;http://schemas.xmlsoap.org/wsdl/&quot;
 xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
 xmlns:soap=&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot;
name=&quot;mainclientconfig&quot;&gt;
    &lt;import location=&quot;TestServiceService.xml&quot; namespace=&quot;http://sample.metro.test/&quot;/&gt;
&lt;/definitions&gt;
</pre></p>
<p style="text-align:left;"><strong>Run client</strong></p>
<p style="text-align:left;">To run the client, execute (while command prompt is in &lt;CLIENT_HOME&gt; directory)<br />
<code>java -cp %CLASSPATH%;.;&lt;METRO_HOME&gt;/lib/webservices-api.jar;&lt;METRO_HOME&gt;/lib/webservices-rt.jar test.metro.client.TestServiceClient</code></p>
<p style="text-align:left;">The output will be something like<br />
<code>Oct 2, 2009 11:14:43 AM [com.sun.xml.ws.policy.jaxws.PolicyConfigParser] parseModel<br />
INFO: WSP1049: Loaded WSIT configuration from file: file:&lt;CLIENT_HOME&gt;/META-INF/wsit-client.xml<br />
Greeatings are...<br />
Hello TheTechTips!</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=49&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/09/30/secure-java-webservice-with-encryption-and-signature/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
		<item>
		<title>Web Service Security</title>
		<link>http://thetechtips.wordpress.com/2009/09/17/web-service-security/</link>
		<comments>http://thetechtips.wordpress.com/2009/09/17/web-service-security/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 15:33:42 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[Webservices]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[Digest]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[jax ws]]></category>
		<category><![CDATA[jaxws]]></category>
		<category><![CDATA[metro]]></category>
		<category><![CDATA[secure]]></category>
		<category><![CDATA[Signature]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[webservice]]></category>
		<category><![CDATA[WS Security]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://thetechtips.wordpress.com/?p=42</guid>
		<description><![CDATA[When considering securing web-service invocations, there are many levels of security that cane be applied. The outermost levels can be viwed as Protocol level security: The security mechanisms used for regular request/response for the particular protocol. For example, Basic Authentication and SSL certificates for HTTP. These mechanisms only ensure that the messages are secured till [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=42&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">When considering securing web-service invocations, there are many levels of security that cane be applied. The outermost levels can be viwed as</p>
<ol style="text-align:left;">
<li>Protocol level security: The security mechanisms used for regular request/response for the particular protocol. For example, Basic Authentication and SSL certificates for HTTP. These mechanisms only ensure that the messages are secured till they reach the protocol (say HTTP) destination. After that, when the actual SOAP message is un secured till it reaches the actual Web service endpoint.<span id="more-42"></span></li>
<li>SOAP message security: In this case the messages are to be secured all the way to the endpoint beyond the protocol level. This is specified by the WS-Security specifications.</li>
</ol>
<p style="text-align:left;"><strong>WS-Security Specification</strong></p>
<p style="text-align:left;"> </p>
<p style="text-align:left;">The WS-Security specification 1.0 was released in 2002. The next version 1.1 was released in 2006. It specifies how to enhance SOAP messages to secure the information exchange.</p>
<p style="text-align:left;">From the WS-Security specification (http<a href="http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=wss#overview">://www.oasis-open.org/committees/tc_home.php?wg_abbrev=wss#overview</a>):</p>
<p style="text-align:left;">&#8220;WS-Security describes enhancements to SOAP messaging to provide quality of protection through message integrity, message confidentiality, and single message authentication. These mechanisms can be used to accommodate a wide variety of security models and encryption technologies.”</p>
<p style="text-align:left;">The WS-Security spec provides SOAP extensions that can be used when building secure web-services. There are three main aspects of security:</p>
<ol style="text-align:left;">
<li><em>Message Authentication</em>WS-Security provides a way of associating <em>security tokens</em> with SOAP messages. These tokens (like Basic Authentication or SSL certificates for HTTP) can be used to identify the sender of the message. The specification does not require use of any particular token. Two commonly used tokens are <em>Username </em>tokens and <em>X509</em> tokens. Other tokens like <em>Kerberos</em> tokens can also be used. These tokens can be included directly in the <em>SOAP message header</em>, or an indirect reference to the token can be sent using digital signature or encryption.</li>
<li><em>Message Integrity </em>Message integrity ensures that the message was not tampered with on its way from the sender to the receiver. WS-Security provides a way to include <em>XML digital signatures</em> in the <em>SOAP</em> message <em>header</em> to help verify integrity. Exactly what parts of the message are signed or what digest/algorithms are used is left to be decided by the web-service provider.</li>
<li><em>Message Confidentiality</em>Message confidentiality means that on the wire, the message should not be readable. WS-Security specifies how the full message or parts of the message can be encrypted and sent so that they are unreadable to attackers. This will use <em>XML encryption</em> and the encryption related information is exchanged in the <em>SOAP</em> <em>header</em>. Either the whole message can be encrypted (like <em>HTTPS</em> transport encryption), or <em>parts</em> of the message are encrypted. Again, the encryption algorithms to be used, the way of exchanging keys (or tokens) is to be decided by the web-service provider.<span id="_marker"> </span></li>
</ol>
<p style="text-align:left;">In the next post, I am going to create a secure web services using JAXWS implementation Metro and create client for the service. Explaining the various types of security in more detail.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=42&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/09/17/web-service-security/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
		<item>
		<title>Problems with EJB3 over iiop on JBoss</title>
		<link>http://thetechtips.wordpress.com/2009/09/02/problems-with-ejb3-over-iiop-on-jboss/</link>
		<comments>http://thetechtips.wordpress.com/2009/09/02/problems-with-ejb3-over-iiop-on-jboss/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:54:04 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[EJB3]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[corba]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[EJB 3]]></category>
		<category><![CDATA[EJB client]]></category>
		<category><![CDATA[EJBObject]]></category>
		<category><![CDATA[iiop]]></category>
		<category><![CDATA[jboss 5]]></category>
		<category><![CDATA[jndi]]></category>
		<category><![CDATA[jnp]]></category>
		<category><![CDATA[lookup]]></category>
		<category><![CDATA[remote]]></category>
		<category><![CDATA[session bean]]></category>

		<guid isPermaLink="false">http://thetechtips.wordpress.com/?p=26</guid>
		<description><![CDATA[In this post I discuss some of the problems I faced when trying to make my EJB3 session beans available over IIOP. The examples use JBoss 4.2.x and 5.x. An EJB3 session bean&#8217;s remote business interface needs to have RMI-IIOP compatible interface according to the EJB3 spec. In addition EJBs should be available over CORBA/IIOP for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=26&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">In this post I discuss some of the problems I faced when trying to make my EJB3 session beans available over IIOP. The examples use JBoss 4.2.x and 5.x.</p>
<p style="text-align:left;">An EJB3 session bean&#8217;s remote business interface needs to have RMI-IIOP compatible interface according to the EJB3 spec. In addition EJBs should be available over CORBA/IIOP for interoperability. The EJB&#8217;s remote home or remote business object obtained over IIOP need to type narrowed instead of plain type casting.<span id="more-26"></span></p>
<p style="text-align:left;">In JBoss the EJBs are not bound to CORBA Naming Service by default. That means if we just deploy annotated EJBs in a ejb-jar file to JBoss, they will not be available over IIOP protocol. Instead the beans are available over JBoss&#8217;s JNP protocol. When trying to access EJBs over JNP, we must use an array jboss client jar files in the client&#8217;s classpath. This works fine in most scenarios, until we want to access from non Java client or in a situation where we have library conflicts.</p>
<p style="text-align:left;"><strong>What works</strong>:</p>
<li style="text-align:left;">Accessing EJB3 over IIOP in JBoss 4.2.x</li>
<li style="text-align:left;">Accessing EJB2 over IIOP in JBoss 4.2.x and 5.x</li>
<p style="text-align:left;"><strong>What does not work</strong>:</p>
<li style="text-align:left;">Accessing EJB3 over IIOP in JBoss 5.x</li>
<p style="text-align:left;"><strong>EJB3 Session Bean</strong>:</p>
<p style="text-align:left;">Because of changes between JBoss 4.2.x and 5.x, the IIOP enabled EJB3 session bean looks different in the two versions.</p>
<p style="text-align:left;">For JBoss <strong>4.2.x</strong>, the interface and bean are</p>
<p><pre class="brush: java;">
package test.ejb3;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface TestSessionRemote extends EJBObject{
 public String greet(String name) throws RemoteException;
}
</pre></p>
<p> </p>
<p><pre class="brush: java;">
package test.ejb3;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import org.jboss.annotation.ejb.RemoteBinding;
import org.jboss.ejb3.iiop.IORFactory;

/**
 * Session Bean implementation class TestSessionBean
 */
@Stateless
@Remote(TestSessionRemote.class)
@RemoteBindings({
 @RemoteBinding(factory=IORFactory.class),
 @RemoteBinding(factory=StatelessRemoteProxyFactory.class)
})
public class TestSessionBean {
     public String greet (String name) {
  if (name != null &amp;amp;&amp;amp; name.trim().length() &amp;gt; 0) {
   return &quot;Hello &quot; + name + &quot;!&quot;;
  } else {
   return &quot;No one to greet.&quot;;
  }
 }
}
</pre></p>
<p style="text-align:left;">Note that according to EJB3 specification, the remote business interface MUST NOT extend EJBObject, but that is allowed in JBoss 4.2.x.  Also, our EJB3 cannot be accessible over IIOP if the remote business interface does not extend EJBObject. This may be a bug or just an unsupported feature. Another thing to notice here is that the bean class does not implement the remote business interface (does not declare that it implements). In JBoss 5 these have been corrected and the remote business interface cannot extend EJBObject. </p>
<p style="text-align:left;">For JBoss <strong>5.x</strong>, the interface and bean are</p>
<p><pre class="brush: java;">
package test.ejb3;

public interface TestSessionRemote {
 public String greet(String name);
}
</pre></p>
<p> </p>
<p><pre class="brush: java;">
package test.ejb3;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import org.jboss.ejb3.annotation.RemoteBinding;
import org.jboss.ejb3.annotation.defaults.RemoteBindingDefaults;

/**
 * Session Bean implementation class TestSessionBean
 */
@Stateless
@Remote(TestSessionRemote.class)
@RemoteBinding(factory=RemoteBindingDefaults.PROXY_FACTORY_IMPLEMENTATION_IOR)
public class TestSessionBean implements TestSessionRemote {

 public String greet (String name) {
  if (name != null &amp;amp;&amp;amp; name.trim().length() &amp;gt; 0) {
   return &quot;Hello &quot; + name + &quot;!&quot;;
  } else {
   return &quot;No one to greet.&quot;;
  }
 }
}
</pre></p>
<p style="text-align:left;">No other configuration is required because we are using annotations to provide all configurations for the beans. These two classes can be directly packaged into a .jar file (with proper directory structure according to package) and deployed to JBoss. Once the JBoss server is started, the deployed EJBs JNDI bindings and Corba Naming bindings can be viewed in jmx-console.</p>
<p style="text-align:left;"><strong>Client</strong>:</p>
<p style="text-align:left;">The reason for using IIOP is that the same client can work on any EJBs available over IIOP.  Below is the client that we use for testing invocation of EJB3 session bean over both IIOP and JNP.</p>
<p><pre class="brush: java;">
package test.ejb3.client;

import java.util.Properties;
import javax.naming.InitialContext;
import test.ejb3.TestSessionRemote;

public class TestClient {
 
 static final String IIOP_PROVIDER_URL = &quot;corbaloc::localhost:3528/JBoss/Naming/root&quot;;
 static final String JNP_PROVIDER_URL = &quot;localhost:1099&quot;;
 static final String JNDI_NAME = &quot;TestSessionBean/remote&quot;;
 
 public static void main (String[] args) {
  TestClient client = new TestClient();
  try {
   client.testJNPInvocation();
  } catch (Exception ex) {
   System.out.println(&quot;Error in JNP test: &quot;+ ex.getMessage());
   ex.printStackTrace();
  }
  try {
   client.testIIOPInvocation();
  } catch (Exception ex) {
   System.out.println(&quot;Error in IIOP test: &quot;+ ex.getMessage());
   ex.printStackTrace();
  }
 }
 
 private void testJNPInvocation() throws Exception{
  Properties props = new Properties();
  props.put(InitialContext.INITIAL_CONTEXT_FACTORY, &quot;org.jnp.interfaces.NamingContextFactory&quot;);
  props.put(InitialContext.PROVIDER_URL, JNP_PROVIDER_URL);
  props.put(InitialContext.URL_PKG_PREFIXES, &quot;org.jboss.naming:org.jnp.interfaces&quot;);
  
  System.out.println(&quot;Running JNP test...&quot;);
  InitialContext intCtx = new InitialContext(props);
  System.out.println(&quot;Found JNP InitialContext&quot;);
  
  Object obj = intCtx.lookup(JNDI_NAME);
  TestSessionRemote remote = (TestSessionRemote) obj;
  System.out.println(&quot;Found JNP EJB Remote&quot;);
  invokeEJB(remote);
 }
 
 private void testIIOPInvocation() throws Exception{
  Properties props = new Properties();
  props.put(InitialContext.INITIAL_CONTEXT_FACTORY, &quot;com.sun.jndi.cosnaming.CNCtxFactory&quot;);
  props.put(InitialContext.PROVIDER_URL, IIOP_PROVIDER_URL);
  
  System.out.println();
  System.out.println(&quot;Running IIOP test...&quot;);
  InitialContext intCtx = new InitialContext(props);
  System.out.println(&quot;Found IIOP InitialContext&quot;);
  
  Object obj = intCtx.lookup(JNDI_NAME);
  TestSessionRemote remote = (TestSessionRemote) javax.rmi.PortableRemoteObject.narrow(obj, TestSessionRemote.class);
  System.out.println(&quot;Found IIOP EJB Remote&quot;); 
  invokeEJB(remote);
 }
 
 private void invokeEJB (TestSessionRemote remote) throws Exception{
   System.out.println(&quot;Invoke result: &quot;+remote.greet(&quot;World&quot;));
 }
}
</pre></p>
<p style="text-align:left;">In order to run this client, the EJB3 interface must be in the classpath. Therefore, create two jar files with only the interface TestSessionRemote in them (two jar files one for each version of the interface above). Let us name the jar files EJB3ExampleJBoss4.jar and EJB3ExampleJBoss5.jar.</p>
<p style="text-align:left;">To run the client, create a directory in desired location (lest call it &lt;CLIENT_DIR&gt;). Under &lt;CLIENT_DIR&gt;, create the directory structure test/ejb3/client/ and put the TestClient.java file there. Place the jar files EJB3ExampleJBoss4.jar and EJB3ExampleJBoss5.jar directly under &lt;CLIENT_DIR&gt;. Now compile and run the client (these steps are for jdk-5): </p>
<ol style="text-align:left;">
<li>cd &lt;CLIENT_DIR&gt;</li>
<li>Compile the client:
<ul>
<li>For JBoss 4.2.x client:<br />
<code>javac -cp %CLASSPATH%; EJB3ExampleJBoss4.jar; &lt;JBOSS4_HOME&gt;\server\default\lib\jboss-j2ee.jar test\ejb3\client\TestClient.java</code></li>
<li>For JBoss 5.x client:<br />
<code>javac -cp %CLASSPATH%; EJB3ExampleJBoss5.jar test\ejb3\client\TestClient.java</code></li>
</ul>
</li>
<li>Run the client: Put the appropriate location for &lt;JBOSS_HOME&gt; below (Jboss4.2.x of 5.x locations for respective clients):
<ul>
<li>For JBoss 4.2.x client:<br />
<code>java -cp %CLASSPATH%; EJB3ExampleJBoss4.jar; &lt;JBOSS4_HOME&gt;\client\jbossall-client.jar -Djava.security.manager -Djava.security.policy=C:\client.policy test.ejb3.client.TestClient</code></li>
<li>For JBoss 5.x client:<br />
<code>java -cp %CLASSPATH%; EJB3ExampleJBoss5.jar; &lt;JBOSS5_HOME&gt;\client\jbossall-client.jar -Djava.security.manager -Djava.security.policy=C:\client.policy test.ejb3.client.TestClient</code></li>
<li>We are using the jbossall-client.jar only for the JNP test. For running just the IIOP test, no JBoss specific files are needed.</li>
</ul>
</li>
<li>Note that the java.security.manager and java.security.policy parameters are needed because we do not have static stubs for the EJB. So the client ORB will need to download the stubs dynamically at runtime. The download will fail with a proper security principal being set. Since we did not enforce any security roles on the server side (in the bean or in config), all roles are allowed. The client.policy file used above just indicates that:<br />
<code>grant { // Allow everything for now        <br />
   permission java.security.AllPermission;<br />
}; </code></li>
</ol>
<p style="text-align:left;">When JBoss4.2.x example is running, the output is:<br />
<code><br />
Running JNP test...<br />
Found JNP InitialContext <br />
<span style="color:#ff0000;">log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).<br />
log4j:WARN Please initialize the log4j system properly.<br />
</span>Found JNP EJB Remote<br />
Invoke result: Hello World!</code></p>
<p><code>Running IIOP test...<br />
Found IIOP InitialContext<br />
Found IIOP EJB Remote<br />
Invoke result: Hello World!</code></p>
<p style="text-align:left;">When JBoss 5.x example is running, the output is:</p>
<p style="text-align:left;"><code>Running JNP test...<br />
Found JNP InitialContext<br />
<span style="color:#ff0000;">log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).<br />
log4j:WARN Please initialize the log4j system properly.<br />
</span>Found JNP EJB Remote<br />
Invoke result: Hello World! </code></p>
<p><code>Running IIOP test...<br />
Found IIOP InitialContext<br />
Error in IIOP test: null<br />
<span style="color:#ff0000;">javax.naming.NameNotFoundException [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]  <br />
at com.sun.jndi.cosnaming.ExceptionMapper.mapException(Unknown Source)  <br />
at com.sun.jndi.cosnaming.CNCtx.callResolve(Unknown Source)  <br />
at com.sun.jndi.cosnaming.CNCtx.lookup(Unknown Source)  <br />
at com.sun.jndi.cosnaming.CNCtx.lookup(Unknown Source)  <br />
at javax.naming.InitialContext.lookup(Unknown Source)  <br />
at test.ejb3.client.TestClient.testIIOPInvocation(TestClient.java:55)  <br />
at test.ejb3.client.TestClient.main(TestClient.java:22)</span></code></p>
<p style="text-align:left;">As we can see, the EJB3 access over IIOP does not work for JBoss 5.x. If anyone has been able to access these EJBs over IIOP while running on JBoss5.x, please correct me. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=26&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/09/02/problems-with-ejb3-over-iiop-on-jboss/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
		<item>
		<title>How to create a Metro JAX-WS portable webservice</title>
		<link>http://thetechtips.wordpress.com/2009/08/27/how-to-create-a-metro-jax-ws-portable-webservice/</link>
		<comments>http://thetechtips.wordpress.com/2009/08/27/how-to-create-a-metro-jax-ws-portable-webservice/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 16:21:37 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[Webservices]]></category>
		<category><![CDATA[jax ws]]></category>
		<category><![CDATA[jaxws]]></category>
		<category><![CDATA[metro]]></category>
		<category><![CDATA[portable]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://thetechtips.wordpress.com/?p=9</guid>
		<description><![CDATA[In this tip, we will go over the steps for creating a webservice using the Metro stack that can be deployed into any web container. This example will deploy it to Tomcat. System Environment JRE 1.5.0_10 Tomcat 5.5.28 Eclipse 3.2 Introduction JAXWS stands for Java API for XML Web Services. It is the new web [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=9&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">In this tip, we will go over the steps for creating a webservice using the Metro stack that can be deployed into any web container. This example will deploy it to Tomcat.<span id="more-9"></span></p>
<p style="text-align:left;"><strong>System Environment</strong></p>
<ol style="text-align:left;">
<li>JRE 1.5.0_10</li>
<li>Tomcat 5.5.28</li>
<li>Eclipse 3.2</li>
</ol>
<p style="text-align:left;"><strong>Introduction</strong></p>
<p style="text-align:left;">JAXWS stands for Java API for XML Web Services. It is the new web services specification for creating and consuming web services in J2EE. It makes use of annotations for creating web services and their clients.</p>
<p style="text-align:left;">JAXB stands for Java API for XML Binding. It is the new xml binding specification for Java to XML and XML to Java binding (again makes use of annotations). The bindings can also be specified using xml files.</p>
<p style="text-align:left;">Metro is Sun&#8217;s web services stack also packaged with the Glassfish server. It includes WSIT (Web Service Interoperability Technologies) in addition to JAXWS-RI and JAXB-RI (Sun&#8217;s reference implementations of the specs).</p>
<p style="text-align:left;"><strong>Creating the Service</strong></p>
<p style="text-align:left;">Before starting, download Metro and extract the libs. Lets create a simple servlet endpoint using a POJO now.</p>
<ol style="text-align:left;">
<li>Create a Dynamic Web Project in Eclipse (lets name it MetroWebserviceSample).</li>
<li>Copy the Metro download&#8217;s webservices-api.jar, webservices-rt.jar and webservices-extra.jar to WEB-INF/lib.</li>
<li>Under the java source directory (the &#8216;src&#8217; directory), create a new package named test.metro.sample.</li>
<li>Create a class TestService.java under test.metro.sample package.  <pre class="brush: java;">

package test.metro.sample;

import javax.jws.WebService;

@WebService
public class TestService {
 public String greet (String name) {
  if (name != null &amp;&amp; name.trim().length() &gt; 0) {
   return &quot;Hello &quot; + name + &quot;!&quot;;
  } else {
   return &quot;No one to greet.&quot;;
  }
 }
}

</pre></li>
<li>Configure WSServlet and WSServletContextListener to web.xml. This servlet should be set up to load on start-up <pre class="brush: xml;">

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app id=&quot;WebApp_ID&quot; version=&quot;2.4&quot; xmlns=&quot;&lt;a href=&quot;http://java.sun.com/xml/ns/j2ee&quot;&gt;http://java.sun.com/xml/ns/j2ee&lt;/a&gt;&quot; xmlns:xsi=&quot;&lt;a href=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/a&gt;&quot; xsi:schemaLocation=&quot;&lt;a href=&quot;http://java.sun.com/xml/ns/j2ee&quot;&gt;http://java.sun.com/xml/ns/j2ee&lt;/a&gt; &lt;a href=&quot;http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;&gt;http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&lt;/a&gt;&quot;&gt;
 &lt;display-name&gt;MetroWebserviceSample&lt;/display-name&gt;
 
	&lt;listener&gt;
	&lt;listener-class&gt;com.sun.xml.ws.transport.http.servlet.WSServletContextListener&lt;/listener-class&gt;
 &lt;/listener&gt;
 &lt;servlet&gt;
    &lt;description&gt;JAX-WS + WSIT endpoint - wsit-enabled-fromjava&lt;/description&gt;
    &lt;display-name&gt;wsit-enabled-fromjava&lt;/display-name&gt;
    &lt;servlet-name&gt;TestWebService&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.sun.xml.ws.transport.http.servlet.WSServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
 &lt;/servlet&gt;
 &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;TestWebService&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/test&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;
&lt;/web-app&gt;

</pre></li>
<li>The WSServlet looks at a config file sun-jaxws.xml to deploy the webservices specified in this config file. Create the following sun-jaxws.xml and put it in the WEB-INF directory <pre class="brush: xml;">

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; 

&lt;endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'&gt;
    &lt;endpoint name='TestService'
        implementation='test.metro.sample.TestService'
        url-pattern='/test'/&gt;
&lt;/endpoints&gt;

</pre></li>
<li>Right click the eclipse project MetroWebserviceSample and export to a .war  file (MetroWebserviceSample.war).</li>
<li>Copy MetroWebserviceSample.war to &lt;TOMCAT_HOME&gt;/webapps and start the Tomcat server.</li>
<li>The WSDL can be looked up at <a href="http://localhost:8080/MetroWebserviceSample/test?wsdl">http://localhost:8080/MetroWebserviceSample/test?wsdl</a>.</li>
</ol>
<p style="text-align:left;">SOAP-UI can be used to quickly test that the webservice is working. Eclipse also has a tool called, &#8216;Web Services Explorer&#8217; which can be used to quickly test webservices. The steps for creating a Java client will be included later.</p>
<p style="text-align:left;">T3</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=9&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/08/27/how-to-create-a-metro-jax-ws-portable-webservice/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
		<item>
		<title>Introduction</title>
		<link>http://thetechtips.wordpress.com/2009/08/26/introduction/</link>
		<comments>http://thetechtips.wordpress.com/2009/08/26/introduction/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 17:12:46 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://thetechtips.wordpress.com/2009/08/26/introduction/</guid>
		<description><![CDATA[I am a Software Engineer with 6 years experience. The internet has been my best friend when it comes to solving the small problems I encounter. Since I have benefitted from the blogs of a lot of people (unable to type the long list here, but Thank You all!), I have decided to start a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=3&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am a Software Engineer with 6 years experience. The internet has been my best friend when it comes to solving the small problems I encounter. Since I have benefitted from the blogs of a lot of people (unable to type the long list here, but Thank You all!), I have decided to start a blog where I can put tech tips that I have learnt over the years. Your suggestions are always welcome.</p>
<p>Will follow up with the first post soon.</p>
<p>T3</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=3&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/08/26/introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://thetechtips.wordpress.com/2009/08/26/hello-world/</link>
		<comments>http://thetechtips.wordpress.com/2009/08/26/hello-world/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 17:06:32 +0000</pubDate>
		<dc:creator>thetechtips</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=1&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Welcome to <a href="http://wordpress.com/">WordPress.com</a>. This is your first post. Edit or delete it and start blogging!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thetechtips.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thetechtips.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thetechtips.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thetechtips.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thetechtips.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thetechtips.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thetechtips.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thetechtips.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thetechtips.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thetechtips.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thetechtips.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thetechtips.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thetechtips.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thetechtips.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thetechtips.wordpress.com&amp;blog=9184781&amp;post=1&amp;subd=thetechtips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thetechtips.wordpress.com/2009/08/26/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e386473a6c716518a9e9ef168ae2a0ca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">thetechtips</media:title>
		</media:content>
	</item>
	</channel>
</rss>
