четверг, 24 июля 2014 г.

APR for MacOS

We can enhance Tomcat's features by using the APR based native library with Tomcat. If we put this library in the Java library path we get better performance, secure session ID support and OS level statistics. The Tomcat documentation explains how to achieve this by compiling sources and the like. But with Mac OSX and MacPorts we don't have to do this ourselves.
With the following steps I was able to add the native library to Tomcat:
  1. Install the Java developer package from Apple's Developer Connection
  2. Add a link with the name include in the /System/Library/Frameworks/JavaVM.framework/Home/ directory. We need this step because later on when we install tomcat-native with MacPorts this path is used as an include for compiling the source files.
    $ ln -s /System/Library/Frameworks/JavaVM.framework/Headers include
  3. $ sudo port install apr
  4. $ sudo port install tomcat-native

After these steps the APR based native libraries are installed in /opt/local/lib. So all we have to do is add this path to the Java library path when we start Tomcat:
$ export CATALINA_OPTS=-Djava.library.path=/opt/local/lib
$ bin/catalina.sh run
Alternatively we can create a symbolic link to add the native library to the default Java installation, so we don't have to specify the /opt/local/libdirectory as a Java library path.
$ sudo ln -s /opt/local/lib/libtcnative-1.* /usr/lib/java


среда, 23 июля 2014 г.

Популярные ошибки при работе с SSL

http://weblogic-wonders.com/weblogic/2010/01/28/troubleshooting-ssl-issues/

вторник, 22 июля 2014 г.

APR for Tomcat in CentOS

Подготовка CentOS:

 yum install  apr apr-devel apr-util
 yum install openssl openssl-devel

С Tomcat поставляются исходники tomcat-native.tar.gz , так же последнию версию можно найти в интернете.

cd /tomcat-native-src/jni/native
./configure --with-apr=/usr/bin/apr-1-config --with-java-home=/usr/java/jdk1.7.0_60/ 
make
make install 


После успешной компиляции необходимо  скопировать библиотеки в /usr/lib64 или /usr/lib в зависимости от OS

среда, 9 июля 2014 г.

2 Источника и 1 Transaction Manager

Java: JDK 7_60 , Spring v4, Hibernate v4, Spring-LDAP v2

Проблема: При работе с 2-мя источниками, проблема с управлением транзакциями.

Решение:

<bean id="jpaUnderLdapTransactionManager" class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndJpaTransactionManager">
    <property name="contextSource" ref="ldapSimpleContextSource"/>
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven transaction-manager="jpaUnderLdapTransactionManager"/>

Class ContextSourceAndJpaTransactionManager

public class ContextSourceAndJpaTransactionManager extends JpaTransactionManager
{

private static final long serialVersionUID = 1L;

private ContextSourceTransactionManagerDelegate ldapManagerDelegate =
        new ContextSourceTransactionManagerDelegate();

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#isExistingTransaction(Object)
 */
protected boolean isExistingTransaction(Object transaction)
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) transaction;

    return super.isExistingTransaction(actualTransactionObject
                                               .getJpaTransactionObject());
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doGetTransaction()
 */
protected Object doGetTransaction() throws TransactionException
{
    Object dataSourceTransactionObject = super.doGetTransaction();
    Object contextSourceTransactionObject =
            ldapManagerDelegate.doGetTransaction();

    return new ContextSourceAndJpaTransactionObject(
            contextSourceTransactionObject, dataSourceTransactionObject
    );
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doBegin(java.lang.Object,
 *      org.springframework.transaction.TransactionDefinition)
 */
protected void doBegin(Object transaction, TransactionDefinition definition)
        throws TransactionException
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) transaction;

    super.doBegin(actualTransactionObject.getJpaTransactionObject(),
                  definition);
    ldapManagerDelegate.doBegin(
            actualTransactionObject.getLdapTransactionObject(),
            definition
    );
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doCleanupAfterCompletion(java.lang.Object)
 */
protected void doCleanupAfterCompletion(Object transaction)
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) transaction;

    super.doCleanupAfterCompletion(actualTransactionObject
                                           .getJpaTransactionObject());
    ldapManagerDelegate.doCleanupAfterCompletion(actualTransactionObject
                                                 .getLdapTransactionObject());
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doCommit(org.springframework.transaction.support.DefaultTransactionStatus)
 */
protected void doCommit(DefaultTransactionStatus status)
        throws TransactionException
{

    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) status.getTransaction();

    try
    {
        super.doCommit(new DefaultTransactionStatus(
                actualTransactionObject.getJpaTransactionObject(),
                status.isNewTransaction(),
                status.isNewSynchronization(),
                status.isReadOnly(),
                status.isDebug(),
                status.getSuspendedResources())
        );
    }
    catch (TransactionException ex)
    {
        if (isRollbackOnCommitFailure())
        {
            logger.debug("Failed to commit db resource, rethrowing", ex);
            // If we are to rollback on commit failure, just rethrow the
            // exception - this will cause a rollback to be performed on
            // both resources.
            throw ex;
        }
        else
        {
            logger.warn(
                    "Failed to commit and resource is rollbackOnCommit not set -"
                            + " proceeding to commit ldap resource.");
        }
    }
    ldapManagerDelegate.doCommit(new DefaultTransactionStatus(
            actualTransactionObject.getLdapTransactionObject(),
            status.isNewTransaction(),
            status.isNewSynchronization(),
            status.isReadOnly(),
            status.isDebug(),
            status.getSuspendedResources())
    );
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doRollback(org.springframework.transaction.support.DefaultTransactionStatus)
 */
protected void doRollback(DefaultTransactionStatus status) throws TransactionException
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) status.getTransaction();

    super.doRollback(new DefaultTransactionStatus(
            actualTransactionObject.getJpaTransactionObject(),
            status.isNewTransaction(),
            status.isNewSynchronization(),
            status.isReadOnly(),
            status.isDebug(),
            status.getSuspendedResources())
    );
    ldapManagerDelegate.doRollback(new DefaultTransactionStatus(
            actualTransactionObject.getLdapTransactionObject(),
            status.isNewTransaction(),
            status.isNewSynchronization(),
            status.isReadOnly(),
            status.isDebug(),
            status.getSuspendedResources())
    );
}

@SuppressWarnings("UnusedDeclaration")
public ContextSource getContextSource()
{
    return ldapManagerDelegate.getContextSource();
}

public void setContextSource(ContextSource contextSource)
{
    ldapManagerDelegate.setContextSource(contextSource);
}

@SuppressWarnings("UnusedDeclaration")
protected void setRenamingStrategy(TempEntryRenamingStrategy renamingStrategy)
{
    ldapManagerDelegate.setRenamingStrategy(renamingStrategy);
}

private final static class ContextSourceAndJpaTransactionObject
{
    private Object ldapTransactionObject;

    private Object jpaTransactionObject;

    public ContextSourceAndJpaTransactionObject(
            Object ldapTransactionObject, Object jpaTransactionObject)
    {
        this.ldapTransactionObject = ldapTransactionObject;
        this.jpaTransactionObject = jpaTransactionObject;
    }

    public Object getJpaTransactionObject()
    {
        return jpaTransactionObject;
    }

    public Object getLdapTransactionObject()
    {
        return ldapTransactionObject;
    }
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doSuspend(java.lang.Object)
 */
protected Object doSuspend(Object transaction) throws TransactionException
{
    throw new TransactionSuspensionNotSupportedException(
            "Transaction manager [" + getClass().getName()
                    + "] does not support transaction suspension");
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doResume(java.lang.Object, java.lang.Object)
 */
protected void doResume(Object transaction, Object suspendedResources)
        throws TransactionException
{
    throw new TransactionSuspensionNotSupportedException(
            "Transaction manager [" + getClass().getName()
                    + "] does not support transaction suspension");
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus)
 */
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status)
{
    super.doSetRollbackOnly(
            new DefaultTransactionStatus(
                ((ContextSourceAndJpaTransactionObject)status.getTransaction())
                        .getJpaTransactionObject(),
                status.isNewTransaction(),
                status.isNewSynchronization(),
                status.isReadOnly(),
                status.isDebug(),
                status.getSuspendedResources())

    );
}
}

P.S: На форумах поодержки spring-ldap пишут что использование 2-х транзакций это зло