-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move JCR Services from Meeds - Meeds-io/meeds#2537 (#332)
- Loading branch information
Showing
134 changed files
with
20,930 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
exo.jcr.component.core/src/main/java/org/exoplatform/services/jdbc/DataSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2011 eXo Platform SAS. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.exoplatform.services.jdbc; | ||
|
||
import org.exoplatform.container.spi.DefinitionByType; | ||
import org.exoplatform.services.jdbc.impl.DataSourceProviderImpl; | ||
|
||
import javax.naming.NamingException; | ||
import javax.sql.DataSource; | ||
|
||
/** | ||
* This provider is used to get a {@link DataSource} in an uniform manner. | ||
* It allows to wrap the {@link DataSource} in case it is defined as managed | ||
* | ||
* @author <a href="mailto:[email protected]">Nicolas Filotto</a> | ||
* @version $Id$ | ||
* | ||
*/ | ||
@DefinitionByType(type = DataSourceProviderImpl.class) | ||
public interface DataSourceProvider | ||
{ | ||
/** | ||
* Try to get the data source from a lookup, if it can't a {@link NamingException} | ||
* will be thrown | ||
* @param dataSourceName the name of the data source to lookup | ||
* @return the {@link DataSource} found thanks to the lookup. The original | ||
* object could be wrap to another {@link DataSource} in order to support | ||
* managed data source. | ||
* @throws NamingException if the data source could not be found | ||
*/ | ||
DataSource getDataSource(String dataSourceName) throws NamingException; | ||
|
||
/** | ||
* Indicates whether or not the given data source is managed | ||
* @param dataSourceName the data source to check | ||
* @return <code>true</code> if the data source is managed, | ||
* <code>false</code> otherwise | ||
*/ | ||
boolean isManaged(String dataSourceName); | ||
} |
182 changes: 182 additions & 0 deletions
182
...mponent.core/src/main/java/org/exoplatform/services/jdbc/impl/DataSourceProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* Copyright (C) 2011 eXo Platform SAS. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.exoplatform.services.jdbc.impl; | ||
|
||
import org.exoplatform.container.xml.InitParams; | ||
import org.exoplatform.container.xml.ValueParam; | ||
import org.exoplatform.container.xml.ValuesParam; | ||
import org.exoplatform.services.jdbc.DataSourceProvider; | ||
import org.exoplatform.services.log.ExoLogger; | ||
import org.exoplatform.services.log.Log; | ||
import org.exoplatform.services.transaction.TransactionService; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.StringTokenizer; | ||
|
||
import javax.naming.InitialContext; | ||
import javax.naming.NamingException; | ||
import javax.sql.DataSource; | ||
import javax.transaction.TransactionManager; | ||
|
||
/** | ||
* The default implementation of {@link DataSourceProvider}. It allows you | ||
* to define a data source as managed thanks to the configuration of this | ||
* component. When the data source is declared as managed, the {@link DataSource} | ||
* object will be wrap into a {@link ManagedDataSource}. | ||
* | ||
* @author <a href="mailto:[email protected]">Nicolas Filotto</a> | ||
* @version $Id$ | ||
* | ||
*/ | ||
public class DataSourceProviderImpl implements DataSourceProvider | ||
{ | ||
|
||
/** | ||
* Logger | ||
*/ | ||
private static final Log LOG = ExoLogger.getLogger("exo.kernel.component.common.DataSourceProviderImpl"); | ||
|
||
/** | ||
* The name of the parameter to know if the tx has to be checked or not. | ||
*/ | ||
protected static final String PARAM_CHECK_TX = "check-tx-active"; | ||
|
||
/** | ||
* The name of the parameter to know if the data sources are always managed. | ||
*/ | ||
protected static final String PARAM_ALWAYS_MANAGED = "always-managed"; | ||
|
||
/** | ||
* The name of the parameter of all the managed data sources. | ||
*/ | ||
protected static final String PARAM_MANAGED_DS = "managed-data-sources"; | ||
|
||
/** | ||
* The transaction manager | ||
*/ | ||
protected final TransactionManager tm; | ||
|
||
/** | ||
* Indicates if the data source needs to check if a tx is active | ||
* to decide if the provided connection needs to be managed or not. | ||
* If it is set to false, the data source will provide only | ||
* managed connections if the data source itself is managed. | ||
*/ | ||
protected boolean checkIfTxActive = true; | ||
|
||
/** | ||
* Indicates that all the data sources are managed | ||
*/ | ||
protected boolean alwaysManaged; | ||
|
||
/** | ||
* A set of all the data sources that are managed | ||
*/ | ||
protected final Set<String> managedDS = new HashSet<String>(); | ||
|
||
/** | ||
* The default constructor | ||
*/ | ||
public DataSourceProviderImpl(InitParams params) | ||
{ | ||
this(params, null); | ||
} | ||
|
||
/** | ||
* The default constructor | ||
*/ | ||
public DataSourceProviderImpl(InitParams params, TransactionService tService) | ||
{ | ||
this.tm = tService == null ? null : tService.getTransactionManager(); | ||
if (params != null) | ||
{ | ||
ValueParam param = params.getValueParam(PARAM_CHECK_TX); | ||
if (param != null) | ||
{ | ||
this.checkIfTxActive = Boolean.valueOf(param.getValue()); | ||
} | ||
param = params.getValueParam(PARAM_ALWAYS_MANAGED); | ||
if (param != null && Boolean.valueOf(param.getValue())) | ||
{ | ||
this.alwaysManaged = true; | ||
return; | ||
} | ||
ValuesParam vp = params.getValuesParam(PARAM_MANAGED_DS); | ||
if (vp != null && vp.getValues() != null) | ||
{ | ||
for (Object oValue : vp.getValues()) | ||
{ | ||
String s = (String)oValue; | ||
StringTokenizer st = new StringTokenizer(s, ","); | ||
while (st.hasMoreTokens()) | ||
{ | ||
String dsName = st.nextToken().trim(); | ||
if (!dsName.isEmpty()) | ||
{ | ||
managedDS.add(dsName); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @throws NamingException | ||
* @see org.exoplatform.services.jdbc.DataSourceProvider#getDataSource(java.lang.String) | ||
*/ | ||
public DataSource getDataSource(String dataSourceName) throws NamingException | ||
{ | ||
InitialContext ctx = new InitialContext(); | ||
try | ||
{ | ||
DataSource ds = (DataSource)ctx.lookup(dataSourceName); | ||
// wrap the data source object if it is managed | ||
return isManaged(dataSourceName) ? new ManagedDataSource(ds, tm, checkIfTxActive) : ds; | ||
} | ||
finally | ||
{ | ||
try | ||
{ | ||
ctx.close(); | ||
} | ||
catch (NamingException e) | ||
{ | ||
LOG.warn("Failed to close naming context.", e); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @see org.exoplatform.services.jdbc.DataSourceProvider#isManaged(java.lang.String) | ||
*/ | ||
public boolean isManaged(String dataSourceName) | ||
{ | ||
if (alwaysManaged) | ||
{ | ||
return true; | ||
} | ||
else if (managedDS.isEmpty()) | ||
{ | ||
return false; | ||
} | ||
return managedDS.contains(dataSourceName); | ||
} | ||
} |
Oops, something went wrong.