Featured FREE Whitepapers

What's New Here?

software-development-2-logo

Big Data: All Aboard the Information Bus

What is good for the short term is often not good for the long term. A simple example from baseball: As it became obvious that steroids enhanced performance, many players turned to the ‘juice’, to drive their short term performance. However, they quickly learned that it was not sustainable. Once you stop or overuse it, your performance and stability begins to breakdown. At the risk of picking on A-Rod, its fairly obvious when you look at a simple graph below. Note: the yellow star is when he admitted to starting to use steroids..So, good for short term…disaster in the long term. Turning to Big Data… I believe an enterprise has to approach this problem with the long term in mind. There are a number of approaches for short term gratification, but they will not scale to support an enterprise economically in the long term. This is why I have alluded to the application server analogy for Big Data a few times (most recently here and here). Let me use some basic graphics to illustrate this analogy. Typical/Simplistic View of an EnterpiseSimply put, an enterprise IT organization invests across all 3 layers and on integration inbetween. However, as I look at emerging Big Data solutions, I’m seeing a pattern that looks like this:The blue bar represents a 3rd party product that provides the entire stack, in proprietary packaging. This approach drives instant gratification. There is a single purchase, the enterprise gets the entire stack, etc. There are new companies that have been successful with this model. But that being said, I am not convinced this is a great approach for an enterprise longer term. Here is why. Unfortunately, enterprises are not as simple as I depicted above. They often look more like this:Enterprises are complex, they evolve, they buy, they build, they customize, etc. If you try to take the ‘proprietary stack’ approach to solving your problems, your enterprise will eventually look like this:This is simply not economical for the medium to long term. With this approach, the enterprise ends up with 10-100′s of full proprietary stacks (the blue bars), and no opportunity to innovate/customize. And worst of all: the enterprise has no leverage in their investments. Every time they need a capability, they have to re-buy capabilities they already have. Like A-Rod in 2002, they will start to regret their short term gratification decisions. To repeat, I understand that some companies have been successful in the short term with the instant gratification model. I just don’t think its the right model for enterprises. This brings us to the application server analogy. Given the data growth (size and variety) that we have seen in the last 3 years and will see in the next 10 years, an enterprise must have a flexible platform for managing that data. Such a platform will leverage all information sources, will annotate/organize that data, will provide real-time analysis, will allow the enterprise to write their own applications or buy 3rd party applications, etc. It will become the ‘Information Bus’ for the enterprise. Todd used the phrase Next Generation Middleware during our exchange on this topic at the Strata Conference panel (see the 22 min mark). Whether its the ‘information bus’ or next generation middleware, the graphic below illustrates the leverage that this approach will give an enterprise.Intelligent enterprises do not make short term gratification decisions en masse. They might make them to solve point problems from time to time. But, it won’t be their strategy. For that reason, I believe we will see Big Data emerge as a true platform…the Information Bus of the future. Don’t forget to share! Reference: Big Data : All Aboard the Information Bus from our JCG partner Rob Thomas at the Rob’s Blog blog....
javaone-logo

JavaOne 2012: What’s New in Groovy 2.0

Guillaume Laforge’s (SpringSource/VMware/@glaforge) ‘What’s New in Groovy 2.0′ presentation was in the same conference room (Hilton Golden Gate 6/7/8) as Martin Odersky’s ‘What’s New in Scala 2.10‘ presentation, but I decided to leave the room between these sessions for three reasons:You tend to get kicked out of a room before the next session anyway to ensure that registered individuals get equal opportunity to ‘the good seats’ (and probably to make sure someone not registered for the session doesn’t displace someone who is registered for the most popular sessions) This is the longer-than-normal one-hour break (no such break tomorrow on the final day of JavaOne 2012) The outside temperature is significantly lower than yesterday and it’s refreshing to stand in a shaded area outside while a cool breeze blows between the buildingsLaforge introduced himself and the way he says his own name with intentional French accent is far cooler than how I pronounce his name. He is the Groovy Project Manager at VMWare, the initiator of the Grails framework, creator of Gaelyk, and a co-author of Groovy in Action. Laforge is another speak who likes to take questions as they come. Laforge listed some features introduced with Groovy 1.8 command chains, gpars (concurrency/parallelism), closures improvements, built-in JSON support, and new AST Transformations. Laforge showed a command chain that looked like English words but is really an alternative combination of methods and parameters (every other word is a method name with the words in between being the parameters to the preceding method name). With some exceptions, command chains allow code to be written without parentheses and some other types of punctuation. He showed several examples of this, including use of commas for multiple arguments. He also explained that no-argument methods do require parentheses. GPars is ‘bundled in the Groovy distribution’ with Groovy 1.8 and ‘covers a wide range of parallel and concurrent paradigms.’ He emphasized that this can be used ‘from plain Java as well!’ GPars is friendlier for Groovy developers. Closure annotation parameters is the ability to use closures within annotations. This is achieved by encoding the closure in the form of a class. Because ‘closures are used a lot in the context of functional programming’ and because Groovy has a ‘functional flavor especially with the use of closures,’ Groovy closures in Groovy 1.8 support closure memoization. This feature potentially brings performance improvements due to caching of results for same parameters to the same closure. More granularity in terms of the number of times to use cache for closure results is available via methods like memoizeAtLeast, memoizeAtMost, and memoizeBetween. Laforge explained that Groovy introduced from the beginning powerful XML marashaling and unmarshaling capabilities ‘back when XML was popular.’ He said it was thus quite natural to add built-in JSON support similar to that XML support. He then showed code samples (embedded within the slides like I prefer) indicating how to use JsonSlurper to consume JSON content and how to use JsonBuilder to generate JSON content. Groovy 1.8 introduced some new Groovy ASTs as discussed in the earlier JavaOne presentation A Walk Through of Groovy’s AST Transformations. He had some nice slides that combined simple text bullets of select AST Transformations with a simple code snippet using that AST Transformation. The new Groovy 1.8 AST Transformations that he covered included @Log, code execution controlling (@ThreadInterrupt for example), @ToString, @EqualsAndHashCode (similar transforms to Project Lombock), @TupleConstructor, @InheritConstructors (replicate all parent class’s constructors such as when writing a class that overrides Exception). At exactly the half-way point in his presentaion, LaForge transitioned to coverage of Groovy 2.0. He stated that it was released in June 2012 and some ‘other dot releases’ have been released since then with ‘bug fixes and so on.’ The key themes of Groovy 2.0 include modularity, JDK 7 alignment, Groovy’s single ‘all’ JAR ‘weighs in at 6 MB,’ but ‘nobody needs everything.’ The new core JAR in Groovy 2.0 is half that sizs (3 MB) and there are module-specific JARs. The ‘big Groovy old JAR’ is still available if you’d like to use it. The effort to make Groovy modular led to the ‘extension modules‘ concept. This allows a developer to ‘contribute instance extension methods.’ This feature works with static type checking and is IDE friendly, two advantages that some of the other Groovy approaches for adding dynamic methods do not equally enjoy. These are structured similarly to Groovy Categories and require a descriptor in META-INF/services/org.codehaus.groovy.runtime.ExtensionModule. Because ‘Groovy supports 99% Java syntax,’ Groovy supports JDK 7 enhancements such as underscores in numeric literals, multicatch exception handling, and better runtime performance due to use of invokedynamic. One of the biggest new features in Groovy 2.0 is static type checking, which Laforge’s first bullet put this way, ‘Goal: make Groovy compiler grumpy!’ Laforge went on to show Groovy code samples with things in the code that will lead to compile-type breakage. He showed off the @TypeChecked annotation. There are aspects of Groovy that cannot be type checked by a compiler. An example of this is the builders prevalent in Groovy. This is what makes using @TypeChecked useful: use it where type checking makes sense and not where it doesn’t make sense. Laforge went on to show how Groovy allows avoidance of explicit casting after an instanceof check as a specific example of Groovy’s type inference. Another Laforge example demonstrated how Groovy’s static type checking catches changing of data type in the same code. Laforge listed several ‘gotchas’ with static type checking. For example, static type checker cannot allow use of dynamic metaClass. Another gotcha example is that the implicit ‘it’ variable in a closure body cannot be assured to be a certain type. There is a Groovy Enhancement Proposal to make it so that this situation can be type checked. It seems that anytime Groovy cannot guarantee the type during static type checking, that situation is not allowed. This does seem to be the safest approach. Advantages of static Groovy code include type safety, faster code, and immunity from monkey patching. The price for this is loss of dynamic features and dynamic dispatch. One audience member asked why one wouldn’t always use static compilation and the answer is that many of us like being able to use the builders and other Groovy dynamic features. You can use the @CompileStatically annotation for portions to be compiled statically. Groovy 1.8 primitive optimizations have dramatically improved performance and Groovy 2.0 static compilation closes gap with Java in Fibbonacci, Pi Quadrature, Binary Trees micro benchmarks. (Laforge did issue the usual caveats about performance benchmarks.) Groovy 2.0 introduces modularity, Project Coin, invokedynamic, static type checking, and static compilation. One interesting side note that Laforge mentioned is that ‘it is possible that Groovy 3 will require JDK 7′ (to use invokedynamic). Laforge’s slides are already posted online. Reference: JavaOne 2012: What’s New in Groovy 2.0 from our JCG partner Dustin Marx at the Inspired by Actual Events blog....
apache-maven-logo

Maven Fluido Skin and Javadoc class diagrams

I have been using Maven sites for a while, and am very happy with it. I didn’t like to have to update my projects after Maven 3, but that was all right, Maven 3 brought many new cool things. However, there were two things that annoyed me: lack of a nice and modern skin, and browsing Javadoc of complex code. The thought of creating a custom Maven skin even crossed my mind, but I never had time to read about it. But the world is full of good and talented people! Like the guys from 99soft. They created Maven Fluido Skin, and donated it to Apache Software Foundation. It’s built on top of Twitter’s Bootstrap and available from Maven central repository. In order to use it in your Maven project, all that you have to do is add the following settings into your src/site/site.xml: <skin> <groupId>org.apache.maven.skins</groupId> <artifactId>maven-fluido-skin</artifactId> <version>1.2.1</version> </skin> Here’s a list of some projects using Maven Fluido Skin (hopefully, in the near future Apache Commons and other projects will adopt this skin as default too ):Maven Fluido Skin tap4j TestLink Java APIRegarding the Javadoc browsing, there’s a nice trick too: add class diagrams. I have seen a new Javadoc template in Apache Commons mailing list, but it was a work in progress, so for now I will stick with class diagrams. These diagrams are generated when you execute the javadoc or the site goals, using graphviz. And there is more. You can click on the diagram classes, as they have a link to the Java class that they reference to. You can find instructions for setting up the diagram generation in Apache Maven web site, or looking at examples (I prefer the latter). But basically, you will need graphviz installed, and something like the following XML snippet in your project pom.xml. <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.7</version> <configuration> <doclet>gr.spinellis.umlgraph.doclet.UmlGraphDoc</doclet> <docletArtifact> <groupId>gr.spinellis</groupId> <artifactId>UmlGraph</artifactId> <version>4.4</version> </docletArtifact> <additionalparam> -inferrel -inferdep -quiet -hide java.* -collpackages java.util.* -qualify -postfixpackage -nodefontsize 9 -nodefontpackagesize 7 -edgefontname 'Trebuchet MS' -nodefontabstractname 'Trebuchet MS' -nodefontclassabstractname 'Trebuchet MS' -nodefontclassname 'Trebuchet MS' -nodefontname 'Trebuchet MS' -nodefontpackagename 'Trebuchet MS' -nodefonttagname 'Trebuchet MS' </additionalparam> </configuration> </plugin> Here’s how a diagram looks like (source: http://tap4j.org/apidocs/index.html):Have fun! And remember to check if your CI machine has graphviz installed too, otherwise you will have 404 in your Javadoc pages. Happy coding and don’t forget to share! Reference: Maven site tips: Maven Fluido Skin and Javadoc class diagrams from our JCG partner Bruno Kinoshita at the Kinoshita’s blog blog....
software-development-2-logo

The Myth of Start Up Costs

Conventional wisdom in the start-up community these days is that it has never been cheaper to start and build a company. It’s been written and re-written about, but I’ll summarize the logic as: You used to have to build your product, then write enormous checks to Oracle, Microsoft, IBM, Sun, etc to buy the hardware/software that you needed to run your business. Then, if you got big enough, you had to build a datacenter. Contrast that with today, where you simply need to sign up on AWS, get a subscription to Workday/SFDC, etc…and the costs of starting up have plummeted. Makes sense. Seems logical. But, it seems to me to be only part of the story. If now is the cheapest time ever to start a company, how do you explain this:Certainly, I could quickly speculate that companies are staying private longer than before and hence, need more capital. However, if you look at the data below, I’m not sure that argument holds up:My belief is that the cost of starting a company is essentially unchanged from 10-20 years ago. If anything, its a bit higher. (Note: obviously this depends on whether you look at year 1 costs or costs until liquidity or somewhere in-between year 1 and liquidity. I’m focusing on the latter two cases). So, why have the costs of starting a company and running it through the mid-years not changed, given that the costs of year 1 are substantially cheaper? I think there has been one substantial change to start-up economics, that very few are acknowledging or writing about: early stage companies have started paying salaries/benefits, etc that are on-par with what Fortune 500 companies pay. I see this over and over again…and its in all parts of the country. This is partially driven by a war on talent, but I don’t think its that simple. I believe the culture and expectations have changed and salaries/benefits are becoming table stakes, as opposed to a nice-to-have. There was a time when you joined a start-up for equity and/or stock options. While there is still some of that, most are joining for salaries/benefits, along with their interest in the mission of the company. This is a fundamental shift in start-up economics and I think it offsets ‘The Myth of Start Up Costs’. I’ll acknowledge that some of my data in this post in anecdotal and I certainly do not have extensive data on private companies. However, I am writing this based on what I’ve seen with my own eyes. Criticism and disagreement is welcome. Reference: The Myth of Start Up Costs from our JCG partner Rob Thomas at the Rob’s Blog blog....
spring-logo

Spring MVC Controller JUnit Testing

JUnit testing Spring MVC controllers is not an easy task. But recently, a new project (to be included in Spring soon) offers new tools to facilitate this. This post illustrates how to test a simple controller via JUnit tests. This code is a variation of the code used in JUnit Testing Spring Service and DAO (with In-Memory Database). It is available from Gihut in the Spring-MVC-JUnit-Testing directory. Test Configuration Classes These are identical to those required for Service and DAO testing. Controller Our controller: @Controller public class MyController {@Autowired private MyService myService;@RequestMapping(value = '/') public String home(Model model) {return 'index';}@RequestMapping(value = '/roundtrip') public String persistenceStatus(Model model) {MilliTimeItem retr = myService.createAndRetrieve(); model.addAttribute('RoundTrip', retr);return 'roundtrip';}}Controller Testing The following creates an instance of MockMvc to test simulated user requests: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={ JpaTestConfig.class, TestConfig.class }) public class MyControllerTest {private MockMvc mockMvc;@Before public void setup() {mockMvc = MockMvcBuilders .annotationConfigSetup(JpaTestConfig.class, TestConfig.class) .build();}@Test public void testHome() throws Exception {mockMvc.perform(get('/')) .andExpect(status().isOk()) .andExpect(forwardedUrl('WEB-INF/pages/index.jsp'));}@Test public void testPersistenceStatus() throws Exception {mockMvc.perform(get('/roundtrip')) .andExpect(status().isOk()) .andExpect(forwardedUrl('WEB-INF/pages/roundtrip.jsp')) .andExpect(model().attributeExists('RoundTrip'));}} The / request tests the returned status and the URL mapping to the JSP page. The /roundtrip request makes sure the returned model does contain the Roundtrip attribute. Dependencies The Spring MVC test artifact is not yet available from maven’s central repository. It should be obtained from another repository: <repositories> <repository> <id>spring.test-mvc</id> <url>http://repo.springsource.org/libs-milestone</url> </repository> </repositories>The required dependency are: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test-mvc</artifactId> <version>1.0.0.M1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency> More Spring related posts here. Reference: Spring MVC Controller JUnit Testing from our JCG partner Jerome Versrynge at the Technical Notes blog....
html5-logo

JavaOne 2012: Building Mobile Apps with HTML5 and Java

I returned to Parc 55 (Mission conference room) to see Max Katz‘s (Exadel Developer Relations) ‘Building Mobile Apps with HTML5 and Java’ Birds-of-a-Feather (BoF) presentation. Specifically, Katz is in Developer Relations for Tiggzi, a cloud-based app platform.Katz only had a single slide before going into demonstration mode. He explained that this is a cloud-based tool as we waited for it to load. He stated by working on an example application to connect to Twitter via their exposed REST interfaces. He pointed out that because this example is based on JavaScript and jQuery, it can be used on any web browser. Katz made his example public and created a TinyUrl for it. Desktops could access http://tinyurl.com/javaone12m and mobile devices could access http://tinyurl.com/javaone12m1. It was pretty impressive to be able to reload/refresh the application as he worked on it on my laptop and on my Droid. It really drove home how easy this cloud-based solution is to use and deploy. It was one of the more impressive real-time demonstrations I’ve seen. Katz moved onto another example that accessed data from the database rather than from Twitter’s API. One of the nifty things he demonstrated was the reporting of the equivalent curl command for accessing the data via REST. All of Katz’s demonstrations were implemented within the Google Chrome web browser. I was surprised at how fluid the behavior was for the most part. It seemed to have responsiveness similar to a decent desktop Java IDE. I’d say that the tool’s interface itself is probably more complicated than the applications most developers would develop on it, but that could probably be said for Java IDEs as well. One of the audience members asked if Tiggzi supports SOAP. The answer was ‘no’ and that Tiggzi supports XML or JSON over REST only. Katz recognized that we ‘don’t want to buy stuff,’ but provided an overview of Tiggzi’s pricing strategy (pricing based on number of deployed applications and all plans have all features). He said that if you use promo code ‘javaone12‘ at tiggzi.com you can get the Pro Plan for a trial period. The title of the session seemed a little misleading as there was next to no coverage of Java other than mention that Twitter uses Java and the first example uses Twitter. The abstract seemed a bit misleading as there was no mention of Tiggzi-specific solution in that abstract. Those points being stated, I still did enjoy the presentation and found it interesting. It’s probably not something I’ll be able to use in the near future, but it was fun to see and think about the possibilities. Reference: JavaOne 2012: Building Mobile Apps with HTML5 and Java [Tiggzi] from our JCG partner Dustin Marx at the Inspired by Actual Events blog....
eclipse-logo

Giving up on Eclipse Juno

In my last blog I posted about my Eclipse 4.2 Juno setup; as much as a reference for me in case I needed to do a reinstall as anything else. What I didn’t talk about then was the issues I’ve been having generally with Juno. I had thought it was my own installation being screwy, but things haven’t improved much since. The main issues I’ve been hitting are:continued modal(!) exception dialogs on certain code completions general sluggishness with the editors HTML editors in particular that are sluggishI could possible live with the last two issues, but the first issue is driving me potty: I can get that modal dialog something like 4 times a minute when I’m into my groove. Not much fun… I’ve realized that I’ve started developing muscle memory to deal with it! As for the performance issues, it would seem that I am not alone, and that things aren’t likely to improve quickly. So, for me, it’s back to Eclipse 3.x. I’ve decided to go with Eclipse 3.7.2; there is an Eclipse 3.8 that was released at the same time as 4.2, but the Eclipse foundation didn’t create the usual distros, and my guess is that it’s not gonna receive much attention if it has bugs etc. Eclipse 3.7 was working just fine for me, so that’s what I’ll use for now. And, let’s see how things are in a year from now. If they don’t fix it, it could be the end of 10 years of using this IDE. PS: I know how this is meant to work: as a user of open source, it is one’s responsibility to contribute back to help improve the product. But my open source time is spent on Apache Isis, and Eclipse is just too big to be able to provide any meaningful help. Don’t forget to share! Reference: Giving up on Eclipse Juno from our JCG partner Dan Haywood at the Dan Haywood blog blog....
news-logo

Spring Shell Project Released

Spring Source released Spring Shell yesterday. Spring Shell is an interactive shell that can be easily extended with commands using a Spring based programming model. It has been extracted from the Spring Roo project by removing the OSGi dependencies and turned into a standalone project. This allows for an easier adoption by those who wish to use only interactive shell features. At the same time, this decoupling facilitates focusing into improving shell functionality and making it an easily reusable component across various projects. Some of its features include:POJO based programming model Spring classpath scanning features Inherits the Roo Shell Features Tab completion Contextual awareness Command Hiding Help assistance Scripting and script recording Command prompt customizationCheck out the Spring Shell page and the relevant documentation. Happy coding!...
play-framework-logo

Play 2.0 framework and XA transactions

XA transactions are useful and out of the box, Play 2.0 today does not have support for them. Here I show how to add that support: First off, some examples when XA is useful: - JPA uses two physical connections if you use entities from two different persistence.xml – those two connections might need to be committed in one transaction, so XA is your only option - Committing a change in a database and at the same time committing a message to JMS. E.g. you want to guarantee that an email is sent after you successfully commit an order to the database, asynchronously. There are other ways, but JMS provides a transactional way to do this with little overhead in having to think about failure. - Writing to a physically different database because of any of several political reasons (legacy system, different department responsible for different database server / different budgets). - See http://docs.codehaus.org/display/BTM/FAQ#FAQ-WhywouldIneedatransactionmanager So the way I see it, XA is something Play needs to ‘support’. Adding support is very easy. I have created a play plugin which is based on Bitronix. Resources are configured in the Bitronix JNDI tree (why on earth does Play use a config file rather than JNDI?! anyway…) You start the transaction like this, ‘withXaTransaction’: def someControllerMethod = Action { withXaTransaction { ctx => TicketRepository.addValidation(user.get, bookingRef, ctx) ValidationRepository.addValidation(bookingRef, user.get, ctx) } val tickets = TicketRepository.getByEventUid(eventUid) Ok(views.html.ticketsInEvent(eventUid, getTickets(eventUid), user, eventValidationForm)) }The ctx object is an XAContext (my own class) which lets you look up resources like a datasource, or set rollback in case of a failure. So the validation repo does this, using ScalaQuery (I used ‘withSession’ rather than ‘withTransaction!’): def addValidation(bookingRef: String, validator: User, ctx: XAContext) = { val ds = ctx.lookupDS("jdbc/maxant/scalabook_admin") Database.forDataSource(ds) withSession { implicit db: Session => Validations.insert(Validation(bookingRef, validator.email, new java.sql.Timestamp(now))) } }And the ticket repo does the following with JMS: def addValidation(user: User, bookingRef: String, ctx: XAContext) = {val xml = {bookingRef} {user.email}val qcf = ctx.lookupCF("jms/maxant/scalabook/ticketvalidations") val qc = qcf.createConnection("ticketValidation","password") val qs = qc.createSession(false, Session.AUTO_ACKNOWLEDGE) val q = qs.createQueue("ticketValidationQueue") //val q = ctx.lookup(QUEUE).asInstanceOf[Queue] val sender = qs.createProducer(q) val m = qs.createTextMessage(xml.toString) sender.send(m) sender.close qs.close qc.close }I’ve tested it with writing to MySQL and sending a JMS message to JBoss (HornetQ) and it seems to work well (except getting hornetQ to play with Bitronix was a bitch – see here: https://community.jboss.org/thread/206180?tstart=0). The scala code for the XA support is: package ch.maxant.scalabook.play20.plugins.xasupportimport play.api.mvc.RequestHeader import play.api.mvc.Results import play.api.mvc.Request import play.api.mvc.AnyContent import play.api.mvc.Result import play.api.mvc.Action import play.api.mvc.Security import play.api._ import play.api.mvc._ import play.api.data._ import play.api.data.Forms._ import ch.maxant.scalabook.persistence.UserRepository import bitronix.tm.TransactionManagerServices import java.util.Hashtable import javax.naming.Context._ import javax.naming.InitialContext import javax.sql.DataSource import bitronix.tm.BitronixTransaction import java.io.File import org.scalaquery.session.Database import org.scalaquery.SQueryException import scala.collection.mutable.ListBuffer import java.sql.Connection import java.sql.SQLException import org.scalaquery.session.Session import bitronix.tm.BitronixTransactionManager import javax.jms.ConnectionFactoryclass XAContext {private val env = new Hashtable[String, String]() env.put(INITIAL_CONTEXT_FACTORY, "bitronix.tm.jndi.BitronixInitialContextFactory") private val namingCtx = new InitialContext(env);var rollbackOnly = false def lookup(name: String) = { namingCtx.lookup(name) } def lookupDS(name: String) = { lookup(name).asInstanceOf[DataSource] } def lookupCF(name: String) = { lookup(name).asInstanceOf[ConnectionFactory] } }trait XASupport { self: Controller =>private lazy val tm = play.api.Play.current.plugin[XASupportPlugin] match { case Some(plugin) => plugin.tm case None => throw new Exception("There is no XASupport plugin registered. Make sure it is enabled. See play documentation. (Hint: add it to play.plugins)") }/** * Use this flow control to make resources used inside `f` commit with the XA protocol. * Conditions: get resources like drivers or connection factories out of the context passed to f. * Connections are opened and closed as normal, for example by the withSession flow control offered * by ScalaQuery / SLICK. */ def withXaTransaction[T](f: XAContext => T): T = { tm.begin//get a ref to the transaction, in case when we want to commit we are no longer on the same thread and TLS has lost the TX. //we have no idea what happens inside f! they might spawn new threads or send work to akka asyncly val t = tm.getCurrentTransaction Logger("XASupport").info("Started XA transaction " + t.getGtrid()) val ctx = new XAContext() var completed = false try{ val result = f(ctx) completed = true if(!ctx.rollbackOnly){ Logger("XASupport").info("committing " + t.getGtrid() + "...") t.commit Logger("XASupport").info("committed " + t.getGtrid()) } result }finally{ if(!completed || ctx.rollbackOnly){ //in case of exception, or in case of set rollbackOnly = true Logger("XASupport").warn("rolling back (completed=" + completed + "/ctx.rollbackOnly=" + ctx.rollbackOnly) t.rollback } } } }class XASupportPlugin(app: play.Application) extends Plugin {protected[plugins] var tm: BitronixTransactionManager = null override def onStart { //TODO how about getting config out of jar! val file = new File(".", "app/bitronix-default-config.properties").getAbsolutePath Logger("XASupport").info("Using Bitronix config at " + file) val prop = System.getProperty("bitronix.tm.configuration", file) //default System.setProperty("bitronix.tm.configuration", prop) //override with default, if not set//start the TM tm = TransactionManagerServices.getTransactionManager Logger("XASupport").info("Started TM with resource config " + TransactionManagerServices.getConfiguration.getResourceConfigurationFilename) }override def onStop { //on graceful shutdown, we want to shutdown the TM too Logger("XASupport").info("Shutting down TM") tm.shutdown Logger("XASupport").info("TM shut down") }}Use the code as you like, I’m giving it away for free :-) Just don’t complain if it don’t work ;-) It would be nice to see this plugin extended and turned into something a little more production ready. Even nicer would be for Play to support a transaction manager natively, including fetching resources out of JNDI. Happy coding and don’t forget to share! Reference: Play 2.0 framework and XA transactions from our JCG partner Ant Kutschera at the The Kitchen in the Zoo blog....
android-logo

Android ListView example with Image and Text

In this tutorial I am going to show you how to create an Android ListView with images and text. You will learn how to load an image from a resource and how to set text to TextView . Here is the screenshot of the finished ListView.Android List View example on Samsung Galaxy Y s5360ItemDetails class will help us to set and get item data : package com.jsupport.listviewimages;public class ItemDetails {public String getName() { return name; } public void setName(String name) { this.name = name; } public String getItemDescription() { return itemDescription; } public void setItemDescription(String itemDescription) { this.itemDescription = itemDescription; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public int getImageNumber() { return imageNumber; } public void setImageNumber(int imageNumber) { this.imageNumber = imageNumber; }private String name ; private String itemDescription; private String price; private int imageNumber;}ItemListBaseAdapter Which is extended from the BaseAdapter and sets item details and the image package com.jsupport.listviewimages;import java.util.ArrayList;import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;public class ItemListBaseAdapter extends BaseAdapter { private static ArrayList<ItemDetails> itemDetailsrrayList; private Integer[] imgid = { R.drawable.p1, R.drawable.bb2, R.drawable.p2, R.drawable.bb5, R.drawable.bb6, R.drawable.d1 }; private LayoutInflater l_Inflater;public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) { itemDetailsrrayList = results; l_Inflater = LayoutInflater.from(context); }public int getCount() { return itemDetailsrrayList.size(); }public Object getItem(int position) { return itemDetailsrrayList.get(position); }public long getItemId(int position) { return position; }public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = l_Inflater.inflate(R.layout.item_details_view, null); holder = new ViewHolder(); holder.txt_itemName = (TextView) convertView.findViewById(R.id.name); holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription); holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price); holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName()); holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription()); holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice()); holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);return convertView; }static class ViewHolder { TextView txt_itemName; TextView txt_itemDescription; TextView txt_itemPrice; ImageView itemImage; } }ListViewImagesActivity package com.jsupport.listviewimages;import java.util.ArrayList;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener;public class ListViewImagesActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);ArrayList<ItemDetails> image_details = GetSearchResults();final ListView lv1 = (ListView) findViewById(R.id.listV_main); lv1.setAdapter(new ItemListBaseAdapter(this, image_details));lv1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int position, long id) { Object o = lv1.getItemAtPosition(position); ItemDetails obj_itemDetails = (ItemDetails)o; Toast.makeText(ListViewImagesActivity.this, 'You have chosen : ' + ' ' + obj_itemDetails.getName(), Toast.LENGTH_LONG).show(); } }); }private ArrayList<ItemDetails> GetSearchResults(){ ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();ItemDetails item_details = new ItemDetails(); item_details.setName('Pizza'); item_details.setItemDescription('Spicy Chiken Pizza'); item_details.setPrice('RS 310.00'); item_details.setImageNumber(1); results.add(item_details);item_details = new ItemDetails(); item_details.setName('Burger'); item_details.setItemDescription('Beef Burger'); item_details.setPrice('RS 350.00'); item_details.setImageNumber(2); results.add(item_details);item_details = new ItemDetails(); item_details.setName('Pizza'); item_details.setItemDescription('Chiken Pizza'); item_details.setPrice('RS 250.00'); item_details.setImageNumber(3); results.add(item_details);item_details = new ItemDetails(); item_details.setName('Burger'); item_details.setItemDescription('Chicken Burger'); item_details.setPrice('RS 350.00'); item_details.setImageNumber(4); results.add(item_details);item_details = new ItemDetails(); item_details.setName('Burger'); item_details.setItemDescription('Fish Burger'); item_details.setPrice('RS 310.00'); item_details.setImageNumber(5); results.add(item_details);item_details = new ItemDetails(); item_details.setName('Mango'); item_details.setItemDescription('Mango Juice'); item_details.setPrice('RS 250.00'); item_details.setImageNumber(6); results.add(item_details);return results; } } Download the complete project: Android ListView Happy coding and don’t forget to share! Reference: Android ListView example with Image and Text from our JCG partner Chathura Wijesinghe at the Java Sri Lankan Support blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close