/*
* Copyright WSO2, Inc. (http://wso2.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.cloud.csg.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.cloud.csg.CSGProxyService;
import org.wso2.carbon.cloud.csg.CSGProxyServiceConfigLayer;
import org.wso2.carbon.cloud.csg.common.CSGCommonUtils;
import org.wso2.carbon.cloud.csg.common.CSGConstant;
import org.wso2.carbon.cloud.csg.common.CSGException;
import org.wso2.carbon.cloud.csg.common.ServiceMetaData;
import org.wso2.carbon.cloud.csg.common.jms.QpidConfigHolder;
import org.wso2.carbon.core.AbstractAdmin;
import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.utils.CarbonUtils;
import java.io.File;
import java.util.StringTokenizer;
/**
* The class <code>CSGAdminService</code> service provides the operations for deploying the proxies
* for out siders. These proxies are the actual proxies that represent the internal
*/
public class CSGAdminService extends AbstractAdmin {
private static final Log log = LogFactory.getLog(CSGAdminService.class);
/**
* Deploy the proxy service
*
* @param metaData meta data associated with this proxy
* @throws CSGException throws in case of an error
*/
public void deployProxy(ServiceMetaData metaData) throws CSGException {
CSGProxyService csgProxyService = new CSGProxyServiceConfigLayer().getCSGServiceProxy();
if (csgProxyService.create(metaData)) {
if (log.isDebugEnabled()) {
log.debug("Proxy service,'" + metaData.getServiceName() + "Proxy" + "'" +
" deployed successfully");
}
} else {
handleException("Cloud not deploy the proxy service, '" +
metaData.getServiceName() + "'");
}
}
/**
* Undeploy the proxy service
*
* @param serviceName the name of the proxy to undeploy
* @throws CSGException thows in case of an error
*/
public void unDeployProxy(String serviceName) throws CSGException {
CSGProxyService csgProxyService = new CSGProxyServiceConfigLayer().getCSGServiceProxy();
if (csgProxyService.destroy(serviceName)) {
if (log.isDebugEnabled()) {
log.info("Proxy service, '" + serviceName + "Proxy" + "'" +
" un-deployed successfully");
}
} else {
handleException("Cloud not un-deploy the proxy service,'" +
serviceName + "'");
}
}
public String getRemoteConnectionURL() {
return QpidConfigHolder.getInstance().getConnectionURL();
}
public void createOrUpdateServerQpidJNDIFile(String qpidJNDIString, String domain)
throws CSGException {
String carbonTenantDir = CarbonUtils.getCarbonTenantsDirPath();
if (carbonTenantDir != null) {
File qpidConfigFile = new File(carbonTenantDir + File.separator + domain +
File.separator + "csg" + File.separator, CSGConstant.QPID_CONFIG_FILE);
StringTokenizer st = new StringTokenizer(qpidJNDIString, "&");
String userName = null, passWord = null;
while (st.hasMoreTokens()) {
String propString = st.nextToken();
String[] temp = propString.split("=");
if (temp[0].equals(CSGConstant.CSG_AMQP_USER_NAME)) {
userName = temp[1];
} else if (temp[0].equals(CSGConstant.CSG_AMQP_PASSWORD)) {
passWord = temp[1];
} else {
CSGCommonUtils.updatePropertyFile(qpidConfigFile, propString);
}
}
String maskedURL = CSGCommonUtils.getMTAwareConnectionURL(userName, passWord, domain,
QpidConfigHolder.getInstance().getConnectionURL());
CSGCommonUtils.updatePropertyFile(qpidConfigFile,
"connectionfactory.QueueConnectionFactory" + "=" + maskedURL);
} else {
handleException("Carbon Tenants directory cloud not be found to write the Qpid JNDI" +
" property file");
}
}
public void createOrUpdateServerRegistryJNDI(String qpidJNDIString) throws CSGException{
org.wso2.carbon.registry.core.Registry registry = getConfigSystemRegistry();
try {
if (!registry.resourceExists(CSGConstant.REGISTRY_CSG_RESOURCE_PATH)) {
Collection collection = registry.newCollection();
registry.put(CSGConstant.REGISTRY_CSG_RESOURCE_PATH, collection);
}
Resource resource = registry.newResource();
resource.setContent(getFinalJNDIString(qpidJNDIString));
registry.put(CSGConstant.REGISTRY_JNDI_RESOURCE_PATH + "/" + "jndi.properties",
resource);
} catch (Exception e) {
handleException("Cloud not update the registry JNDI entries of the JNDI string '" +
qpidJNDIString + "'", e);
}
}
private String getFinalJNDIString(String jndiString) {
StringTokenizer st = new StringTokenizer(jndiString, "&");
String newJNDIString = "";
String userName = null, passWord = null;
while (st.hasMoreTokens()) {
String propString = st.nextToken();
String[] temp = propString.split("=");
if (temp[0].equals("userName")) {
userName = temp[1];
} else if (temp[0].equals("passWord")) {
passWord = temp[1];
} else {
if (newJNDIString.equals("")) {
newJNDIString = propString;
} else {
newJNDIString = newJNDIString + "\n" + propString;
}
}
}
String maskedURL = maskTheURL(QpidConfigHolder.getInstance().getConnectionURL(),
userName, passWord);
return "connectionfactory.QueueConnectionFactory=" + maskedURL + "\n" + newJNDIString;
}
private String maskTheURL(String originalURL, String userName, String passWord){
// FIXME- we may need to read the direct connection url given the username
return originalURL.replace(CSGConstant.CSG_AMQP_USER_NAME,userName).
replace(CSGConstant.CSG_AMQP_PASSWORD, passWord);
}
private void handleException(String msg) throws CSGException {
log.error(msg);
throw new CSGException(msg);
}
private void handleException(String msg, Throwable t) throws CSGException {
log.error(msg, t);
throw new CSGException(msg, t);
}
}