Enabling Spring Security Expression-based Access Control for methods in a Spring Roo app

Expression-based Access Control allows you to annotate specific methods with access rules. To enable, add the following element to your webmvc-config.xml file for your Roo webapp (not the security context file, it must be in the context file for the web app):

<security:global-method-security pre-post-annotations="enabled"/>

The explanation for why this needs to be in your webapp context is covered here.

Adding a Mysql datasource to JBoss AS 7

I haven’t used JBoss since 4.x days. Seems adding a datasource is a few steps more complicated than it used to be. To summarize this post, the steps you need are:

  1. create a com/mysql/main dir under /jobss-as-7.1.final-install-dir/modules
  2. drop your mysql connector in this dir
  3. create a module.xml file in the same dir with the following content:
  4. <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="com.mysql">
      <resources>
        <resource-root path="mysql-connector-java-5.1.18-bin.jar"/>
      </resources>
      <dependencies>
        <module name="javax.api"/>
      </dependencies>
    </module>
  5. modify your jboss-install-dir/standalone/configuration/standalone.xml file and define your datasource by adding a section like this to the subsystem datasources section:
    <datasource jndi-name="java:jboss/datasources/MysqlDS" pool-name="MysqlDS" enabled="true" use-java-context="true">
                        <connection-url>jdbc:mysql://localhost:3306/your_db_name</connection-url>
                        <driver>mysql</driver>
                        <security>
                            <user-name>your_userid</user-name>
                            <password>your_password</password>
                        </security>
                    </datasource>
                    <drivers>
                        <driver name="mysql" module="com.mysql">
                            <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                        </driver>
                    </drivers>

Understanding the ignorance behind avoiding new software development technologies

New technologies are avoided by companies for many reasons, the primary reason maybe to avoid the risk of the unknown. If your success of your company/business/project depends on the success of your software development project, then of course you want to minimize risks associated with the latest and greatest new technologies especially if they’re so new that there’s no success stories from others already using them. Sometimes this risk is worth taking if the potential reward is so great that it negates the downsides of the risk. For example if the competitive advantage that could potentially be gained outweighs the significance of the risk, then maybe the risk is worth taking.

When new techologies are avoided due to lack of understanding, lack of information, incorrect assumptions, and beliefs based on false information, then you’ve got a more serious problem on your hands. The problem with any decision based on false information worsens when the decision makers believe the information they are basing their decisions on to be correct. The higher up the management chain these decisions are made the worse this situation becomes for two reasons:

  1. no-one questions the decisions made because of the decision maker’s level of authority
  2. the decision maker loses credibility with their team because they are seen to be making wrong/poor decisions based on bad information

‘Community’ or ‘tribal’ knowledge is a dangerous thing when decisions are made and opinions formed based on bad information. Opinions and beliefs quickly spread within a group/team/project of any size, and once those opinions and beliefs take hold as ‘fact’ then the rot has already set in from within – it can be hard to undo this damage and replace this bad information with true and correct facts.

The trouble with believing facts to be true is that if you are wrong, you probably don’t realize you are wrong. The more time that goes by operating with false information, the worse the situation can become, as you poison the knowledge of those around you too. The poor/incorrect information spreads, and beliefs become truths.

In an industry where having correct and up to date information is key to what we do everyday and to be successful, we owe it to ourselves to help anyone who is operating with false information. The longer this situation continues, the worse it becomes, so the best thing to do is to stop the situation in it’s tracks as soon as you see this starting. When success depends on having true and correct information, its important to stamp out sources of bad information as soon as you can.

 

Configuring a Spring web app using JPA2 to use a JBoss datasource on OpenShift

By default, when creating a web app with Spring Roo, your JPA configuration is set up to use a Commons DBCP BasicDataSource. This works great for testing locally on Tomcat, but doesn’t work when deployed to the OpenShift environment, you’ll get errors like this as Hibernate tries to get connections and set up your schema based on your Entity mappings :

2012/03/08 18:17:03,423 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport]
(MSC service thread 1-3) HHH000231: Schema export unsuccessful:
java.lang.UnsupportedOperationException: The application must supply JDBC connections

At the current time there isn’t a great deal of documentation on the Open Shift site that explains how to config your app to use a datasource, but this post on the community forum has some good clues (this one is Flex specific, but it mentions the standalone.xml JBoss config file).

When you add a database cartridge to your app in OpenShift, your JBoss in your environment is also configured with a DataSource using connection properties set up to access your MySQL (or other) db, and it’s ready to go.

To get your Spring Roo app configured to use the datasource requires a bit more effort. There’s various posts online about how to configure JPA2 to use a DataSource provided by a container, but getting all the right parts changed or removed in your existing config is a bit tricky. This post here lists all the steps needed, and most of the text below is taken from this post (thanks to the poster of this entry on the Spring forum as before I got to this stage I had already spent a few hours trying to piece this together) – here we go:

Edit src/main/resources/META-INF/spring/applicationContext.xml:

  • remove the BasicDataSource bean (the DataSource is now specified in persistence.xml)
  • remove the JpaTransactionManager bean
  • add <tx:jta-transaction-manager /> to look up JBoss’ JtaTransactionManager from JNDI instead. The default name of the bean using this tag is ‘transactionManager’
  • If you had previously used the <tx:annotation-driven> tag, make sure it’s transaction-manger attribute now uses ‘transactionManager’ (from the previous step):
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

 

  • remove the LocalContainerEntityManagerFactoryBean
  • add this bean:
<jee:jndi-lookup id="entityManagerFactory"
        jndi-name="your/app/MyEntityManagerFactory"
        expected-type="javax.persistence.EntityManagerFactory" />

Your EntityManagerFactory gets exposed by JBoss from the following changes to your persistence.xml files:

Edit src/main/resources/META-INF/persistence.xml:

  • change the persistence-unit’s “transaction-type” attribute from RESOURCE_LOCAL to JTA
  • add a child element to “persistence-unit” as follows:
<jta-data-source>java:/MyDSName</jta-data-source>

Add the following property to expose your EntityManagerFactory:

<property name="jboss.entity.manager.factory.jndi.name"
        value="your/app/MyEntityManagerFactory"/>