use of com.sun.messaging.jmq.jmsserver.cluster.api.ClusterManager in project openmq by eclipse-ee4j.
the class ChangeClusterMasterBrokerHandler method handle.
/**
* Handle the incomming administration message.
*
* @param con The Connection the message came in on.
* @param cmd_msg The administration message
* @param cmd_props The properties from the administration message
*/
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
int status = Status.OK;
String emsg = null;
if (DEBUG) {
logger.log(Logger.INFO, this.getClass().getName() + ": " + cmd_props);
}
boolean notificationOnly = false, fromJMSRA = false;
String oldmb = (String) cmd_props.get(MessageType.JMQ_CLUSTER_OLD_MASTER_BROKER);
String newmb = (String) cmd_props.get(MessageType.JMQ_CLUSTER_NEW_MASTER_BROKER);
Object val = cmd_props.get(MessageType.JMQ_JMSRA_MANAGED_BROKER);
if (val != null && Boolean.parseBoolean(val.toString())) {
fromJMSRA = true;
}
val = cmd_props.get(MessageType.JMQ_JMSRA_NOTIFICATION_ONLY);
if (val != null && Boolean.parseBoolean(val.toString()) && fromJMSRA && Globals.isJMSRAManagedBroker()) {
notificationOnly = true;
}
logger.log(logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_ADMIN_RECEIVED_CMD, MessageType.getString(MessageType.CHANGE_CLUSTER_MASTER_BROKER) + "[" + MessageType.JMQ_CLUSTER_NEW_MASTER_BROKER + "=" + newmb + ", " + MessageType.JMQ_CLUSTER_OLD_MASTER_BROKER + "=" + oldmb + "]" + (fromJMSRA ? "JMSRA" : "") + (notificationOnly ? "(" + MessageType.JMQ_JMSRA_NOTIFICATION_ONLY + ")" : "")));
if (Globals.getHAEnabled()) {
status = Status.PRECONDITION_FAILED;
emsg = rb.getKString(rb.E_OP_NOT_APPLY_TO_HA_BROKER, MessageType.getString(MessageType.CHANGE_CLUSTER_MASTER_BROKER));
logger.log(Logger.ERROR, emsg);
sendReply(status, emsg, con, cmd_msg);
return true;
}
if (Globals.useSharedConfigRecord()) {
status = Status.PRECONDITION_FAILED;
emsg = rb.getKString(rb.E_OP_NOT_APPLY_NO_MASTER_BROKER_MODE, MessageType.getString(MessageType.CHANGE_CLUSTER_MASTER_BROKER));
logger.log(Logger.ERROR, emsg);
sendReply(status, emsg, con, cmd_msg);
return true;
}
try {
BrokerStateHandler.setExclusiveRequestLock(ExclusiveRequest.CHANGE_MASTER_BROKER);
} catch (Throwable t) {
status = Status.PRECONDITION_FAILED;
if (t instanceof BrokerException) {
status = ((BrokerException) t).getStatusCode();
}
emsg = MessageType.getString(MessageType.CHANGE_CLUSTER_MASTER_BROKER) + ": " + Status.getString(status) + " - " + t.getMessage();
logger.log(Logger.ERROR, emsg);
status = Status.PRECONDITION_FAILED;
sendReply(status, emsg, con, cmd_msg);
return true;
}
try {
if (!Globals.dynamicChangeMasterBrokerEnabled()) {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_NO_SUPPORT_DYNAMIC_CHANGE_MASTER_BROKER), Status.NOT_ALLOWED);
}
if (newmb == null) {
throw new IllegalArgumentException("null " + MessageType.JMQ_CLUSTER_NEW_MASTER_BROKER);
}
if (!fromJMSRA && Globals.isJMSRAManagedBroker()) {
throw new IllegalAccessException(Globals.getBrokerResources().getKString(BrokerResources.X_ADMIN_CHANGE_MASTER_NOT_FROM_JMSRA));
}
ClusterManager cm = Globals.getClusterManager();
BrokerMQAddress self = (BrokerMQAddress) cm.getMQAddress();
BrokerMQAddress master = (cm.getMasterBroker() == null ? null : (BrokerMQAddress) cm.getMasterBroker().getBrokerURL());
BrokerMQAddress newmba = BrokerMQAddress.createAddress(newmb);
BrokerMQAddress oldmba = null;
if (oldmb != null) {
oldmba = BrokerMQAddress.createAddress(oldmb);
}
if (notificationOnly) {
if (master == null) {
emsg = "IllegalStateException for notification " + MessageType.getString(MessageType.CHANGE_CLUSTER_MASTER_BROKER) + ": No master broker";
logger.log(logger.ERROR, emsg);
sendReply(Status.ERROR, emsg, con, cmd_msg);
Broker.getBroker().exit(1, emsg, BrokerEvent.Type.ERROR);
throw new IllegalStateException(emsg);
}
if (newmba.equals(self)) {
if (!master.equals(self)) {
emsg = "IllegalStateException for notification " + MessageType.getString(MessageType.CHANGE_CLUSTER_MASTER_BROKER) + ": This broker, which has master broker " + master + ", is not the master broker as expected";
logger.log(logger.ERROR, emsg);
sendReply(Status.ERROR, emsg, con, cmd_msg);
Broker.getBroker().exit(1, emsg, BrokerEvent.Type.ERROR);
return true;
}
}
if (oldmba != null && oldmba.equals(self)) {
if (!master.equals(newmba)) {
emsg = "IllegalStateException for notification " + MessageType.getString(MessageType.CHANGE_CLUSTER_MASTER_BROKER) + ": This broker, which is the old master broker " + oldmba + ", does not have " + newmba + " as the master broker as expected";
logger.log(logger.ERROR, emsg);
sendReply(Status.ERROR, emsg, con, cmd_msg);
Broker.getBroker().exit(1, emsg, BrokerEvent.Type.ERROR);
return true;
}
}
sendReply(Status.OK, null, con, cmd_msg);
return true;
}
if (master == null) {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_CLUSTER_NO_MASTER_BROKER_REJECT_CHANGE_MASTER), Status.PRECONDITION_FAILED);
}
if (newmba.equals(master)) {
logger.log(logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_CLUSTER_CHANGE_MASTER_BROKER_SAME, newmba));
sendReply(Status.OK, null, con, cmd_msg);
return true;
}
if (oldmba == null) {
oldmba = master;
}
if (!oldmba.equals(master)) {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_CLUSTER_CHANGE_MASTER_BROKER_MISMATCH, oldmba.toString(), master), Status.PRECONDITION_FAILED);
}
if (!self.equals(master)) {
if (!Globals.isJMSRAManagedBroker()) {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_CLUSTER_THIS_BROKER_NOT_MASTER_BROKER_REJECT_CHANGE_MASTER, master.toString()), Status.NOT_ALLOWED);
}
sendReply(Status.OK, null, con, cmd_msg);
return true;
}
Globals.getClusterBroadcast().changeMasterBroker(newmba, oldmba);
sendReply(Status.OK, null, con, cmd_msg);
return true;
} catch (Exception e) {
status = Status.ERROR;
emsg = e.getMessage();
if (e instanceof BrokerException) {
status = ((BrokerException) e).getStatusCode();
emsg = emsg + "[" + Status.getString(status) + "]";
}
logger.logStack(Logger.ERROR, emsg, e);
sendReply(status, emsg, con, cmd_msg);
return true;
} finally {
BrokerStateHandler.unsetExclusiveRequestLock(ExclusiveRequest.CHANGE_MASTER_BROKER);
}
}
use of com.sun.messaging.jmq.jmsserver.cluster.api.ClusterManager in project openmq by eclipse-ee4j.
the class ClusterSubscriptionInfo method getGPacket.
public GPacket getGPacket(short protocol, boolean changeRecord) {
assert (subscription != null);
assert (protocol == ProtocolGlobals.G_NEW_INTEREST || protocol == ProtocolGlobals.G_DURABLE_ATTACH || protocol == ProtocolGlobals.G_REM_DURABLE_INTEREST);
if (changeRecord) {
assert (protocol == ProtocolGlobals.G_NEW_INTEREST || protocol == ProtocolGlobals.G_REM_DURABLE_INTEREST);
}
GPacket gp = null;
switch(protocol) {
case ProtocolGlobals.G_NEW_INTEREST:
ClusterConsumerInfo cci = ClusterConsumerInfo.newInstance(subscription, null);
gp = cci.getGPacket(protocol);
if (changeRecord) {
gp.putProp("N", subscription.getDurableName());
String clientID = subscription.getClientID();
gp.putProp("I", (clientID == null ? "" : clientID));
gp.putProp("M", Boolean.TRUE);
gp.putProp("shared", Boolean.valueOf(subscription.getShared()));
gp.putProp("jmsshared", Boolean.valueOf(subscription.getJMSShared()));
} else {
ChangeRecordInfo cri = subscription.getCurrentChangeRecordInfo(ProtocolGlobals.G_NEW_INTEREST);
if (cri != null) {
gp.putProp("shareccSeq" + 1, cri.getSeq());
gp.putProp("shareccUUID" + 1, cri.getUUID());
gp.putProp("shareccResetUUID" + 1, cri.getResetUUID());
}
}
break;
case ProtocolGlobals.G_DURABLE_ATTACH:
{
assert (consumer != null);
gp = GPacket.getInstance();
gp.setType(protocol);
String dname = subscription.getDurableName();
if (dname != null) {
gp.putProp("N", dname);
}
String clientID = subscription.getClientID();
gp.putProp("I", (clientID == null ? "" : clientID));
String ndsubname = subscription.getNDSubscriptionName();
if (ndsubname != null) {
gp.putProp("NDN", ndsubname);
}
ClusterManager cm = Globals.getClusterManager();
int csize = 1;
if (cm != null) {
csize = cm.getConfigBrokerCount();
if (csize <= 0) {
csize = 1;
}
}
int prefetch = consumer.getPrefetchForRemote() / csize;
if (prefetch <= 0) {
prefetch = 1;
}
gp.putProp(consumer.getConsumerUID().longValue() + ":" + Consumer.PREFETCH, Integer.valueOf(prefetch));
gp.putProp("allowsNonDurable", Boolean.TRUE);
c.marshalBrokerAddress(c.getSelfAddress(), gp);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
try {
ClusterConsumerInfo.writeConsumer(consumer, dos);
dos.flush();
bos.flush();
} catch (IOException e) {
/* Ignore */
}
gp.setPayload(ByteBuffer.wrap(bos.toByteArray()));
}
break;
case ProtocolGlobals.G_REM_DURABLE_INTEREST:
{
gp = GPacket.getInstance();
gp.setType(protocol);
gp.putProp("C", Integer.valueOf(1));
String dname = subscription.getDurableName();
String clientID = subscription.getClientID();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
try {
dos.writeUTF(dname);
dos.writeUTF((clientID == null ? "" : clientID));
dos.flush();
bos.flush();
} catch (IOException e) {
/* Ignore */
}
gp.setPayload(ByteBuffer.wrap(bos.toByteArray()));
if (changeRecord) {
gp.putProp("N", dname);
gp.putProp("I", (clientID == null ? "" : clientID));
gp.putProp("M", Boolean.TRUE);
} else {
ChangeRecordInfo cri = subscription.getCurrentChangeRecordInfo(ProtocolGlobals.G_REM_DURABLE_INTEREST);
if (cri != null) {
gp.putProp("shareccSeq" + 1, cri.getSeq());
gp.putProp("shareccUUID" + 1, cri.getUUID());
gp.putProp("shareccResetUUID" + 1, cri.getResetUUID());
}
}
}
break;
}
return gp;
}
use of com.sun.messaging.jmq.jmsserver.cluster.api.ClusterManager in project openmq by eclipse-ee4j.
the class MQWebSocketServiceApp method checkOrigin.
private boolean checkOrigin(HttpRequestPacket request) throws Exception {
String origin = request.getHeader(Constants.ORIGIN_HEADER);
List<URL> origins = service.getAllowedOrigins();
URL myurl = service.getMyURL();
if (MQWebSocket.getDEBUG()) {
logger.log(Logger.INFO, getClass().getSimpleName() + ".checkOrigin(" + request + "): origin=" + origin + ", myurl=" + myurl + ", allowedOrigins=" + origins);
}
if (origin == null || origins == null) {
return true;
}
URL ou = null;
try {
ou = new URL(origin);
} catch (Exception e) {
logger.log(logger.ERROR, br.getKString(br.X_WEBSOCKET_INVALID_CLIENT_ORIGIN, origin));
return false;
}
if (!ou.getProtocol().equals(myurl.getProtocol())) {
return false;
}
if (ou.getHost().equals(myurl.getHost())) {
return true;
}
InetAddress oia = InetAddress.getByName(ou.getHost());
if (oia.equals(InetAddress.getByName(myurl.getHost()))) {
return true;
}
ClusterManager cm = Globals.getClusterManager();
Iterator itr = cm.getConfigBrokers();
ClusteredBroker cb = null;
BrokerMQAddress addr = null;
String h;
while (itr.hasNext()) {
cb = (ClusteredBroker) itr.next();
addr = (BrokerMQAddress) cb.getBrokerURL();
h = addr.getHost().getCanonicalHostName();
if (MQWebSocket.getDEBUG()) {
logger.log(logger.INFO, getClass().getSimpleName() + ".checkOrigin(" + request + "), origin=" + ou + ", check configured cluster broker " + addr + "[" + h + "]");
}
if (ou.getHost().equals(h)) {
return true;
}
if (oia.equals(addr.getHost())) {
return true;
}
}
URL url;
Iterator<URL> itr1 = origins.iterator();
while (itr1.hasNext()) {
url = itr1.next();
if (!ou.getProtocol().equals(url.getProtocol())) {
continue;
}
if (ou.getHost().equals(url.getHost())) {
return true;
}
if (oia.equals(InetAddress.getByName(url.getHost()))) {
return true;
}
}
logger.log(logger.ERROR, br.getKString(br.X_WEBSOCKET_ORIGIN_NOT_ALLOWED, origin, origins));
return false;
}
use of com.sun.messaging.jmq.jmsserver.cluster.api.ClusterManager in project openmq by eclipse-ee4j.
the class PortMapper method allowConnection.
/**
*****************************************************
* Implement PUServiceCallback interface
*******************************************************************
*/
@Override
public boolean allowConnection(InetSocketAddress sa, boolean ssl) {
if (DEBUG) {
logger.log(logger.INFO, "PortMapper.alllowConnection(" + sa + ", " + ssl + "), sslEnabled=" + sslEnabled + ", allowedHosts=" + allowedHosts + ", broker=" + Globals.getBrokerInetAddress());
}
if (!sslEnabled) {
return true;
}
if (ssl) {
return true;
}
if (sa.getAddress().equals(Globals.getBrokerInetAddress())) {
return true;
}
if (sa.getAddress().isLoopbackAddress()) {
return true;
}
if (allowedHosts.contains(sa.getAddress())) {
return true;
}
ClusterManager cm = Globals.getClusterManager();
Iterator itr = cm.getConfigBrokers();
ClusteredBroker cb = null;
BrokerMQAddress addr = null;
while (itr.hasNext()) {
cb = (ClusteredBroker) itr.next();
addr = (BrokerMQAddress) cb.getBrokerURL();
if (DEBUG) {
logger.log(logger.INFO, "PortMapper.allowConnection(" + sa + "), check configured cluster broker " + addr);
}
if (addr.getHost().equals(sa.getAddress())) {
return true;
}
}
return false;
}
use of com.sun.messaging.jmq.jmsserver.cluster.api.ClusterManager in project openmq by eclipse-ee4j.
the class UpdateClusterBrokerListHandler method handle.
/**
* Handle the incomming administration message.
*
* @param con The Connection the message came in on.
* @param cmd_msg The administration message
* @param cmd_props The properties from the administration message
*/
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
int status = Status.OK;
String msg = null;
if (DEBUG) {
logger.log(Logger.INFO, this.getClass().getName() + ": " + cmd_props);
}
if (Globals.getHAEnabled()) {
status = Status.ERROR;
msg = rb.getKString(rb.E_OP_NOT_APPLY_TO_HA_BROKER, MessageType.getString(MessageType.UPDATE_CLUSTER_BROKERLIST));
logger.log(Logger.ERROR, msg);
} else if (!Globals.isJMSRAManagedBroker()) {
status = Status.ERROR;
msg = rb.getKString(rb.E_BROKER_NOT_JMSRA_MANAGED_IGNORE_OP, MessageType.getString(MessageType.UPDATE_CLUSTER_BROKERLIST));
logger.log(Logger.ERROR, msg);
msg = "BAD REQUEST";
} else {
try {
ClusterManager cm = Globals.getClusterManager();
MQAddress self = cm.getMQAddress();
String brokerlist = (String) cmd_props.get(MessageType.JMQ_CLUSTER_BROKERLIST);
Set brokers = cm.parseBrokerList(brokerlist);
MQAddress master = (cm.getMasterBroker() == null ? null : cm.getMasterBroker().getBrokerURL());
logger.log(logger.INFO, rb.getKString(rb.I_UPDATE_BROKERLIST, self + (master == null ? "]" : "(" + cm.CONFIG_SERVER + "=" + master + ")"), "[" + brokerlist + "]"));
if (master != null && !brokers.contains(master)) {
msg = rb.getKString(rb.X_REMOVE_MASTERBROKER_NOT_ALLOWED, master.toString(), brokers.toString() + "[" + brokerlist + "]");
throw new BrokerException(msg);
}
if (!brokers.contains(self)) {
brokerlist = "";
}
Properties prop = new Properties();
prop.put(cm.AUTOCONNECT_PROPERTY, brokerlist);
BrokerConfig bcfg = Globals.getConfig();
bcfg.updateProperties(prop, true);
} catch (PropertyUpdateException e) {
status = Status.BAD_REQUEST;
msg = e.getMessage();
logger.log(Logger.WARNING, msg);
} catch (Exception e) {
status = Status.ERROR;
msg = e.toString();
logger.log(Logger.WARNING, msg);
}
}
// Send reply
Packet reply = new Packet(con.useDirectBuffers());
reply.setPacketType(PacketType.OBJECT_MESSAGE);
setProperties(reply, MessageType.UPDATE_CLUSTER_BROKERLIST_REPLY, status, msg);
parent.sendReply(con, cmd_msg, reply);
return true;
}
Aggregations