org.glassfish.jms.admin.cli.CreateJMSResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
//Portions Copyright [2018-2021] Payara Foundation and/or affiliates
package org.glassfish.jms.admin.cli;
import com.sun.appserv.connectors.internal.api.ConnectorsUtil;
import com.sun.enterprise.config.serverbeans.*;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.util.SystemPropertyConstants;
import fish.payara.enterprise.config.serverbeans.DeploymentGroup;
import org.glassfish.api.ActionReport;
import org.glassfish.api.I18n;
import org.glassfish.api.Param;
import org.glassfish.api.admin.*;
import org.glassfish.config.support.CommandTarget;
import org.glassfish.config.support.TargetType;
import org.glassfish.connectors.config.AdminObjectResource;
import org.glassfish.connectors.config.ConnectorConnectionPool;
import org.glassfish.connectors.config.ConnectorResource;
import org.glassfish.hk2.api.PerLookup;
import org.jvnet.hk2.annotations.Service;
import jakarta.inject.Inject;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
/**
* Create JMS Resource Command
*
*/
@Service(name="create-jms-resource")
@PerLookup
@I18n("create.jms.resource")
@ExecuteOn({RuntimeType.DAS})
@TargetType({CommandTarget.DAS,CommandTarget.STANDALONE_INSTANCE,CommandTarget.CLUSTER,CommandTarget.DOMAIN, CommandTarget.DEPLOYMENT_GROUP})
@RestEndpoints({
@RestEndpoint(configBean=Resources.class,
opType=RestEndpoint.OpType.POST,
path="create-jms-resource",
description="create-jms-resource")
})
public class CreateJMSResource implements AdminCommand {
@Param(name="resType")
String resourceType;
@Param(optional=true, defaultValue="true")
Boolean enabled;
@Param(name="property", optional=true, separator=':')
Properties props;
@Param(optional=true)
String target = SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME;
@Param(name="description", optional=true)
String description;
@Param(optional=true, defaultValue="false")
Boolean force;
@Param(name="jndi_name", primary=true)
String jndiName;
@Inject
CommandRunner commandRunner;
@Inject
Domain domain;
//ConnectorConnectionPool[] connPools;
private static final String QUEUE = "jakarta.jms.Queue";
private static final String TOPIC = "jakarta.jms.Topic";
private static final String QUEUE_CF = "jakarta.jms.QueueConnectionFactory";
private static final String TOPIC_CF = "jakarta.jms.TopicConnectionFactory";
private static final String UNIFIED_CF = "jakarta.jms.ConnectionFactory";
private static final String DEFAULT_JMS_ADAPTER = "jmsra";
private static final String DEFAULT_OPERAND="DEFAULT";
private static final String JNDINAME_APPENDER="-Connection-Pool";
/* As per new requirement all resources should have unique name so appending 'JNDINAME_APPENDER' to jndiName
for creating jndiNameForConnectionPool.
*/
private String jndiNameForConnectionPool;
//JMS destination resource properties
private static final String NAME = "Name";
private static final String IMQ_DESTINATION_NAME = "imqDestinationName";
final private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(CreateJMSResource.class);
/**
* Executes the command with the command parameters passed as Properties
* where the keys are the paramter names and the values the parameter values
*
* @param context information
*/
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
//Collection connPools = domain.getResources().getResources(ConnectorConnectionPool.class);
if (resourceType == null) {
report.setMessage(localStrings.getLocalString("create.jms.resource.noResourceType",
"No Resoruce Type specified for JMS Resource."));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
if (jndiName == null) {
report.setMessage(localStrings.getLocalString("create.jms.resource.noJndiName",
"No JNDI name specified for JMS Resource."));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
if (!(resourceType.equals(TOPIC_CF) || resourceType.equals(QUEUE_CF) || resourceType.equals(UNIFIED_CF) || resourceType.equals(TOPIC) || resourceType.equals(QUEUE))) {
report.setMessage(localStrings.getLocalString("create.jms.resource.InvalidResourceType",
"Invalid Resource Type specified for JMS Resource."));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
jndiNameForConnectionPool = jndiName + JNDINAME_APPENDER;
if (force) {
Resource res = null;
if (resourceType.equals(TOPIC) || resourceType.equals(QUEUE))
res = ConnectorsUtil.getResourceByName(domain.getResources(), AdminObjectResource.class, jndiName);
else
res = ConnectorsUtil.getResourceByName(domain.getResources(), ConnectorResource.class, jndiName);
if (res != null) {
ActionReport deleteReport = report.addSubActionsReport();
ParameterMap parameters = new ParameterMap();
parameters.set(DEFAULT_OPERAND, jndiName);
parameters.set("target", target);
commandRunner.getCommandInvocation("delete-jms-resource", deleteReport, context.getSubject()).parameters(parameters).execute();
if (ActionReport.ExitCode.FAILURE.equals(deleteReport.getActionExitCode())) {
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
}
//Populate the JMS RA map
populateJmsRAMap();
/* Map MQ properties to Resource adapter properties */
if (props != null) {
Enumeration en = props.keys();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String raKey = getMappedName(key);
if (raKey == null) raKey = key;
props.put(raKey, (String) props.get(key));
if(! raKey.equals(key))
props.remove(key);
}
}
ActionReport subReport = report.addSubActionsReport();
if (resourceType.equals(TOPIC_CF) || resourceType.equals(QUEUE_CF) || resourceType.equals(UNIFIED_CF)) {
ConnectorConnectionPool cpool = (ConnectorConnectionPool) ConnectorsUtil.getResourceByName(
domain.getResources(), ConnectorConnectionPool.class, jndiNameForConnectionPool);
boolean createdPool = false;
// If pool is already existing, do not try to create it again
if (cpool == null || ! filterForTarget (jndiNameForConnectionPool)) {
// Add connector-connection-pool.
ParameterMap parameters = populateConnectionPoolParameters();
commandRunner.getCommandInvocation("create-connector-connection-pool", subReport, context.getSubject()).parameters(parameters).execute();
createdPool= true;
if (ActionReport.ExitCode.FAILURE.equals(subReport.getActionExitCode())){
report.setMessage(localStrings.getLocalString("create.jms.resource.cannotCreateConnectionPool",
"Unable to create connection pool."));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
ParameterMap params = populateConnectionResourceParameters();
commandRunner.getCommandInvocation("create-connector-resource", subReport, context.getSubject()).parameters(params).execute();
if (ActionReport.ExitCode.FAILURE.equals(subReport.getActionExitCode())){
report.setMessage(localStrings.getLocalString("create.jms.resource.cannotCreateConnectorResource",
"Unable to create connection resource."));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
//rollback the connection pool ONLY if we created it...
if (createdPool) {
ParameterMap paramsForRollback = new ParameterMap();
paramsForRollback.set(DEFAULT_OPERAND, jndiNameForConnectionPool);
commandRunner.getCommandInvocation("delete-connector-connection-pool", subReport, context.getSubject())
.parameters(paramsForRollback)
.execute();
}
return;
}
} else if (resourceType.equals(TOPIC) ||
resourceType.equals(QUEUE))
{
ParameterMap aoAttrList = new ParameterMap();
try{
//validate the provided properties and modify it if required.
Properties properties = validateDestinationResourceProps(props, jndiName);
//aoAttrList.put("property", properties);
StringBuilder builder = new StringBuilder();
for (java.util.Map.Entry