alfresco-community-repo/source/java/org/alfresco/util/OpenOfficeConnectionTester.java
Derek Hulley 306e626cec Merged V2.2 to HEAD (V2.1 sourced)
7127: Merged V2.1 to V2.2
      7118: Fixed overly-eager applicability of patches brought forward from previous releases
      7119: Fixed SQL script patch schema numbering
   7245: Merged V2.1 to V2.2:
      7238: Serializes alfresco->alfresco deployments to the same target.
      7241: Added AVM index tracking into the built-in, cron-controlled config.
      7242: More DEBUG messages for index tracking, where required.
      7243: Fix for url encoding issue as found by by Ishii Akinori
   7372: Merged V2.1 to V2.2
      7289: Fix for AWC-1542 where utf-8 characters not displaying correctly in RSS feed output
      7300: Bumped up session size management values to reduce potential issues with mix-style, shorter transactions.
      7303: Portlet updates for MSIE problems in Liferay.
      7304: Added the <cifs-url-suffix>. AWC-1671.
      7317: Fix OO shutdown
      7319: Catch for raising rule executions using null NodeRefs.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@7374 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2007-11-13 03:19:12 +00:00

139 lines
4.5 KiB
Java

/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.util;
import java.net.ConnectException;
import net.sf.jooreports.openoffice.connection.OpenOfficeConnection;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.i18n.I18NUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
/**
* A bootstrap class that checks for the presence of a valid <b>OpenOffice</b> connection, as provided by the
* <code>net.sf.jooreports.openoffice.connection.OpenOfficeConnection</code> implementations.
*
* @author Derek Hulley
*/
public class OpenOfficeConnectionTester extends AbstractLifecycleBean
{
private static final String INFO_CONNECTION_VERIFIED = "system.openoffice.info.connection_verified";
private static final String ERR_CONNECTION_FAILED = "system.openoffice.err.connection_failed";
private static Log logger = LogFactory.getLog(OpenOfficeConnectionTester.class);
private OpenOfficeConnection connection;
private boolean strict;
public OpenOfficeConnectionTester()
{
this.strict = false;
}
/**
* @param connection the <b>OpenOffice</b> connection.
*/
public void setConnection(OpenOfficeConnection connection)
{
this.connection = connection;
}
/**
* @param strict set to <tt>true</tt> to generate a failure if the connection is not connected
* during the {@link #checkConnection() connection check}, or false to just issue
* a warning. The default is <tt>false</tt>.
*/
public void setStrict(boolean strict)
{
this.strict = strict;
}
/**
* @see #checkConnection()
*/
@Override
protected void onBootstrap(ApplicationEvent event)
{
checkConnection();
}
/**
* Disconnect
*/
@Override
protected void onShutdown(ApplicationEvent event)
{
if(connection != null)
{
if(connection.isConnected())
{
connection.disconnect();
}
}
}
/**
* Perform the actual connection check. If this component is {@link #setStrict(boolean) strict},
* then a disconnected {@link #setConnection(OpenOfficeConnection) connection} will result in a
* runtime exception being generated.
*/
private synchronized void checkConnection()
{
PropertyCheck.mandatory(this, "connection", connection);
String connectedMessage = I18NUtil.getMessage(INFO_CONNECTION_VERIFIED);
if (connection.isConnected())
{
// the connection is fine
logger.info(connectedMessage);
return;
}
// attempt to make the connection
try
{
connection.connect();
// that worked
logger.info(connectedMessage);
return;
}
catch (ConnectException e)
{
// no luck
}
// now we have to either fail or report the connection
String msg = I18NUtil.getMessage(ERR_CONNECTION_FAILED);
if (strict)
{
throw new AlfrescoRuntimeException(msg);
}
else
{
logger.warn(msg);
}
}
}