Mac application menu for java program that also runs on windows - java

I am working on a program that will work on both Windows and Mac machines. I found this link that explains how to implement the application menu on a Mac. But since it uses mac specific classes for interfaces I am not sure how to write the class so it compiles in Windows as well.

I can recommend JavaFx. The interface will look like windows on windows and like mac on mac. You can use normal java for everything that works on every machine. In addition, the Jfx Scene builder can help you with designing the application.

I should have googled a little further. Found this class on a old google group. Just had to extend it to include Preferences.
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class OSXAdapter implements InvocationHandler {
private OSXQuitListener _quitListener;
private OSXAboutListener _aboutListener;
private OSXPreferenceListener _perferenceListener;
/**
* creates this adapter, only does stuff when we're on a mac, if it's unable to
* register the quit adapter, then we throw an exception.
*
* #throws ClassNotFoundException
* #throws SecurityException
* #throws NoSuchMethodException
* #throws IllegalArgumentException
* #throws IllegalAccessException
* #throws InvocationTargetException
*/
#SuppressWarnings({ "rawtypes", "unchecked" })
public OSXAdapter() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
// get's the os name
String vers = System.getProperty("os.name").toLowerCase();
// only attempt to the do the following if we're on a mac
if (vers.indexOf("mac") != -1) {
Class quitHandlerClass = Class.forName("com.apple.mrj.MRJQuitHandler");
Class aboutHandlerClass = Class.forName("com.apple.mrj.MRJAboutHandler");
Class prefHandlerClass = Class.forName("com.apple.mrj.MRJPrefsHandler");
Class mrjapputilsClass = Class.forName("com.apple.mrj.MRJApplicationUtils");
Object methodHandler = Proxy.newProxyInstance(quitHandlerClass.getClassLoader(), new Class[] { quitHandlerClass, aboutHandlerClass, prefHandlerClass }, this);
Method appUtilsObj = mrjapputilsClass.getMethod("registerQuitHandler", new Class[] { quitHandlerClass });
appUtilsObj.invoke(null, new Object[] { methodHandler });
appUtilsObj = mrjapputilsClass.getMethod("registerAboutHandler", new Class[] { aboutHandlerClass });
appUtilsObj.invoke(null, new Object[] { methodHandler });
appUtilsObj = mrjapputilsClass.getMethod("registerPrefsHandler", new Class[] { prefHandlerClass });
appUtilsObj.invoke(null, new Object[] { methodHandler });
}
}
/**
* registers an about dialog. When the os x system fires the event which
* triggers an about class
*
* #param listener
*/
public void setAboutListener(OSXAboutListener listener) {
_aboutListener = listener;
}
/**
* registers an preference listener. When the os x fires the preference event this will be
* fired.
*
* #param listener
*/
public void setPerferenceListener(OSXPreferenceListener listener) {
_perferenceListener = listener;
}
/**
* register an quit listener. When the os x fires the quit event this will be
* fired.
*
* #param listener
*/
public void setQuitListener(OSXQuitListener listener) {
_quitListener = listener;
}
/**
* #see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
* java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
if (meth.getName().equals("handleQuit")) {
if (null != _quitListener) {
_quitListener.handleQuit();
}
} else if (meth.getName().equals("handleAbout")) {
if (null != _aboutListener) {
_aboutListener.handleAbout();
}
} else if (meth.getName().equals("handlePrefs")) {
if (null != _perferenceListener) {
_perferenceListener.handlePrefs();
}
}
return null;
}
/**
* listener which listens to the about event from the os x
* system
*
* #author Chris Shorrock
*/
public interface OSXAboutListener {
/**
* handles the about display event.
*/
public void handleAbout();
}
/**
* this listener is fired when the os x system quits
*
* #author Chris Shorrock
*/
public interface OSXQuitListener {
/**
* this method is called when os x tells this application
* to quit.
*/
public void handleQuit();
}
/**
* this listener is fired when the os x system fires preferences
*
* #author Chris Shorrock
*/
public interface OSXPreferenceListener {
/**
* this method is called when os x tells this application
* to open preferences.
*/
public void handlePrefs();
}
}

Related

Spring amqp consumer not re-connecting to queue after network failure

We have a spring application which is having a dynamic queue listener connecting to queue in rabbitmq. Let's say I have a total of 5 listener consumer connected to 5 queues from my spring application to rabbitmq.
Now if Network fluctuation/failure happens then each time, first one of my 5 connected queues is stopped retrying to rabbitmq.
I have debugged code through spring-amqp class and found out that on creating a connection with rabbitmq (when network failure happens), it fails to connect to it and throw org.springframework.amqp.AmqpIOException particular exception which is not handled in retry function so that that queue is removed from retried queue list.
My Main class:
#Slf4j
#SpringBootApplication(exclude = {ClientAutoConfiguration.class})
#EnableTransactionManagement
#EnableJpaRepositories(basePackages = "com.x.x.repositories")
#EntityScan(basePackages = "com.x.x.entities")
public class Main
{
#PostConstruct
void configuration()
{
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
/**
* The main method.
*
* #param args the arguments
*/
public static void main(String[] args)
{
ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
RabbitMQListenerUtil queueRegisterUtil = context.getBean(RabbitMQListenerUtil.class);
try
{
queueRegisterUtil.registerSpecifiedListenerForAllInstance();
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
}
}
Class which is used to create 5 consumer/listener
/**
* The Class RabbitMQListenerUtil.
*/
#Component
#Slf4j
public class RabbitMQListenerUtil
{
#Autowired
private ApplicationContext applicationContext;
public void registerSpecifiedListenerForAllInstance()
{
try
{
log.debug("New Listener has been register for instane name : ");
Thread.sleep(5000);
registerNewListener("temp1");
registerNewListener("temp2");
registerNewListener("temp3");
registerNewListener("temp4");
registerNewListener("temp5");
}
catch (Exception e)
{
}
}
/**
* This method will add new listener bean for given queue name at runtime
*
* #param queueName - Queue name
* #return Configurable application context
*/
public void registerNewListener(String queueName)
{
AnnotationConfigApplicationContext childAnnotaionConfigContext = new AnnotationConfigApplicationContext();
childAnnotaionConfigContext.setParent(applicationContext);
ConfigurableEnvironment environmentConfig = childAnnotaionConfigContext.getEnvironment();
Properties listenerProperties = new Properties();
listenerProperties.setProperty("queue.name", queueName + "_queue");
PropertiesPropertySource pps = new PropertiesPropertySource("props", listenerProperties);
environmentConfig.getPropertySources().addLast(pps);
childAnnotaionConfigContext.register(RabbitMQListenerConfig.class);
childAnnotaionConfigContext.refresh();
}
}
Class which create dynamic listener for queue consumer
/**
* The Class RabbitMQListenerConfig.
*/
#Configuration
#Slf4j
#EnableRabbit
public class RabbitMQListenerConfig
{
/** The Constant ALLOW_MESSAGE_REQUEUE. */
private static final boolean ALLOW_MESSAGE_REQUEUE = true;
/** The Constant MULTIPLE_MESSAGE_FALSE. */
private static final boolean MULTIPLE_MESSAGE_FALSE = false;
/**
* Listen.
*
* #param msg the msg
* #param channel the channel
* #param queue the queue
* #param deliveryTag the delivery tag
* #throws IOException Signals that an I/O exception has occurred.
*/
#RabbitListener(queues = "${queue.name}")
public void listen(Message msg, Channel channel, #Header(AmqpHeaders.CONSUMER_QUEUE) String queue, #Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException
{
int msgExecutionStatus = 0;
try
{
String message = new String(msg.getBody(), StandardCharsets.UTF_8);
log.info(message);
}
catch (Exception e)
{
log.error(e.toString());
log.error(e.getMessage(), e);
}
finally
{
ackMessage(channel, deliveryTag, msgExecutionStatus);
}
}
/**
* Ack message.
*
* #param channel the channel
* #param deliveryTag the delivery tag
* #param msgExecutionStatus the msg execution status
* #throws IOException Signals that an I/O exception has occurred.
*/
protected void ackMessage(Channel channel, long deliveryTag, int msgExecutionStatus) throws IOException
{
if (msgExecutionStatus == Constants.MESSAGE_DELETE_FOUND_EXCEPTION)
{
channel.basicNack(deliveryTag, MULTIPLE_MESSAGE_FALSE, ALLOW_MESSAGE_REQUEUE);
}
else
{
channel.basicAck(deliveryTag, MULTIPLE_MESSAGE_FALSE);
}
}
/**
* Bean will create from this with given name.
*
* #param name - Queue name-
* #return the queue
*/
#Bean
public Queue queue(#Value("${queue.name}") String name)
{
return new Queue(name);
}
/**
* RabbitAdmin Instance will be created which is required to create new Queue.
*
* #param cf - Connection factory
* #return the rabbit admin
*/
#Bean
public RabbitAdmin admin(ConnectionFactory cf)
{
return new RabbitAdmin(cf);
}
}
Application log :
https://pastebin.com/NQWdmdTH
I have tested this multiple times and each time my first connected queue is being stopped from connecting .
========================= UPDATE 1=============================
Code to reconnect stopped consumer :
https://pastebin.com/VnUrhdLP
Caused by: java.net.UnknownHostException: rabbitmqaind1.hqdev.india
There is something wrong with your network.

Spring-boot No thread-bound request found when throw exception on JMS queue listener

I am trying to consume an AWS queue using Spring boot with JMS, and I am having a problem throwing exceptions in my consumer method.
Every time I try to throw a custom exception in my consumer method, to log into an Aspect, the following message is returned:
errorCause=java.lang.IllegalStateException: No thread-bound request
found: Are you referring to request attributes outside of an actual
web request, or processing a request outside of the originally
receiving thread? If you are actually operating within a web request
and still receive this message, your code is probably running outside
of DispatcherServlet/DispatcherPortlet: In this case, use
RequestContextListener or RequestContextFilter to expose the current
request., errorMessage=Error listener queue,
date=2018-06-29T17:45:26.290, type=InvoiceRefuseConsumer]
I have already created a RequestContextListener bean but I did not succeed.
Could someone tell me what might be causing this error?
Here is my code:
Module 1 - Queue consumer
#Service
public class InvoiceRefuseConsumer extends AbstractQueue implements IQueueConsumer{
#Autowired
private InvoiceRefuseService invoiceRefuseService;
#JmsListener(destination = "${amazon.sqs.queue-to-be-consumed}")
#Override
public void listener(#Payload String message) throws ApplicationException {
try {
//Convert the payload received by the queue to the InvoiceFuseParam object
InvoiceRefuseParam param = convertToPojo(message, InvoiceRefuseParam.class);
// Set the type and reason of the refused invoice
param.setType(InvoiceRefuseType.INVOICE_TREATMENT.getId());
if(param.getReasonCode().equals(InvoiceRefuseTypeOperationType.TYPE_OPERATION_INSERT.getDesc())) {
// Persist data information
invoiceRefuseService.save(param);
} else if(param.getReasonCode().equals(InvoiceRefuseTypeOperationType.TYPE_OPERATION_DELETE.getDesc())) {
// Remove refused invoice
invoiceRefuseService.delete(param.getKeyAccess(), param.getType());
}
} catch(Exception e) {
throw new ApplicationException("Error listener queue", e);
}
}
}
Module 2 - Service operations
#Service
public class InvoiceRefuseService {
/**
* automatically initiates the InvoiceRefuseCrud
*/
#Autowired
private InvoiceRefuseCrud invoiceRefuseCrud;
/**
* automatically initiates the SupplierCrud
*/
#Autowired
private SupplierCrud supplierCrud;
/**
* automatically initiates the SequenceDao
*/
#Autowired
private SequenceDao sequenceDao;
/**
* automatically initiates the InvoiceRefuseDao
*/
#Autowired
private InvoiceRefuseDao invoiceRefuseDao;
/**
* automatically initiates the OrderConsumerService
*/
#Autowired
private OrderConsumerService orderConsumerService;
/**
* automatically initiates the InvoiceOrderService
*/
#Autowired
private InvoiceOrderService invoiceOrderService;
/**
* automatically initiates the BranchWarehouseTypeDao
*/
#Autowired
private BranchWarehouseTypeDao branchWarehouseTypeDao;
/**
* Method created to delete a invoice refuse
* #param key
* #param type
* #throws ApplicationException
*/
#Transactional
public void delete(String key, int type) throws ApplicationException {
try {
// Search for the refused invoices
List<InvoiceRefuseModel> lsInvoiceRefuseModel = invoiceRefuseCrud.findBykeyAccessAndType(key, type);
if(ApplicationUtils.isEmpty(lsInvoiceRefuseModel)){
throw new FieldValidationException(getKey("key.notfound"));
}
// Remove refused invoice and cascate with the the scheduling order
invoiceRefuseCrud.deleteAll(lsInvoiceRefuseModel);
} catch (Exception e) {
throw new ApplicationException(getKey("api.delete.error"), e);
}
}
/**
* Method created to save a new invoice refuse
* #param param
* #throws ApplicationException
*/
#OneTransaction
public void save(InvoiceRefuseParam param) throws ApplicationException {
try {
for (String orderNumber : param.getOrderNumbers()) {
// Verify if the invoice refused key already exists
Optional.ofNullable(invoiceRefuseCrud.findBykeyAccessAndType(param.getKeyAccess(), param.getType()))
.filter(invoiceRefuses -> invoiceRefuses.isEmpty())
.orElseThrow(() -> new ApplicationException(getKey("invoice.alread.exists")));
// Convert to model
InvoiceRefuseModel model = convertToSaveModel(param, orderNumber);
// Save data on database
InvoiceRefuseModel result = invoiceRefuseCrud.save(model);
// Associate new refused invoice with the scheduled order
associateInvoiceRefusedToSchedulingOrder(result);
}
} catch (Exception e) {
throw new ApplicationException(getKey("api.save.error"), e);
}
}
/**
* Method creates to associate a refused invoice to the scheduling order
* #param invoiceRefuseModel
* #throws ApplicationException
*/
public void associateInvoiceRefusedToSchedulingOrder(InvoiceRefuseModel invoiceRefuseModel) throws ApplicationException{
// Search for the scheduled order
List<InvoiceOrderModel> lsInvoiceOrderModel = invoiceOrderService.findByNuOrder(invoiceRefuseModel.getNuOrder());
for (InvoiceOrderModel orderModel : lsInvoiceOrderModel) {
// Verify if its a SAP order
boolean isOrderSap = Optional
.ofNullable(branchWarehouseTypeDao.findByIdBranch(orderModel.getNuReceiverPlant()))
.filter(branch -> branch.getNaLoadPoint() != null)
.isPresent();
if (isOrderSap) {
// Update the order status
invoiceOrderService.updateStatus(orderModel);
}
}
}
/**
* Method created to convert from param to model
* #param param
* #param orderNumber
* #return InvoiceRefuseModel
* #throws ApplicationException
*/
private InvoiceRefuseModel convertToSaveModel(InvoiceRefuseParam param, String orderNumber) throws ApplicationException{
OrderParam orderParam = new OrderParam();
orderParam.getLsOrdeNumber().add(orderNumber);
// Search for SAP orders
OrderDataPojo orderSap = Optional.ofNullable(orderConsumerService.findAll(orderParam))
.filter(ordersSap -> ordersSap.getOrders().size() > 0)
.orElseThrow(() -> new ApplicationException(getKey("ordersap.notfound")));
// Convert to model
InvoiceRefuseModel model = new InvoiceRefuseModel();
model.setNuOrder(orderNumber);
model.setCdCompany(BranchMatrixType.MATRIX.getCdCompany());
model.setDsMessage(param.getReasonDescription());
model.setDtIssue(param.getIssueDate());
model.setKeyAccess(param.getKeyAccess());
model.setNuGuid(param.getGuid());
model.setNuInvoice(param.getInvoiceNumber() + param.getInvoiceSerialNumber());
model.setTsCreation(new Date());
model.setNuInvoiceSerial(param.getInvoiceSerialNumber());
model.setNuIssuerPlant(orderSap.getOrders().stream().map(o -> o.getHeader().getIssuerPlant()).findFirst().get());
model.setNuReceiverPlant(orderSap.getOrders().stream().map(o -> o.getHeader().getReceiverPlant()).findFirst().get());
model.setType(param.getType());
model.setCdInvoiceRefuseMessage(param.getReasonCode());
// Passing these fields is required for refused invoices, but they are not received for notes in treatment
if(param.getType().equals(InvoiceRefuseType.INVOICE_REFUSED.getId())) {
model.setIsEnableReturn(BooleanType.getByBool(param.getIsEnableReturn()).getId());
model.setDtRefuse(param.getRefuseDate());
}
// Search for the issuing supplier
SupplierModel supplierModelIssuer = findSupplier(param.getDocumentIdIssuer());
model.setCdSupplierIssuer(supplierModelIssuer.getCdSupplier());
// Search for the receiver supplier
SupplierModel supplierModelReceiver = findSupplier(param.getDocumentIdIssuer());
model.setCdSupplierReceiver(supplierModelReceiver.getCdSupplier());
// Set the primary key
InvoiceRefuseModelId id = new InvoiceRefuseModelId();
id.setCdInvoiceRefuse(sequenceDao.nextIntValue(SequenceName.SQ_INVOICE_REFUSE));
model.setId(id);
return model;
}
/**
* Method created to search for a supplier
* #param documentId
* #return SupplierModel
* #throws ApplicationException
*/
private SupplierModel findSupplier(String documentId) throws ApplicationException{
// Search for the supplier
SupplierModel model = supplierCrud.findTop1ByNuDocumentIdAndCdCompany(documentId, BranchMatrixType.MATRIX.getCdCompany());
if(model == null){
throw new ApplicationException(getKey("supplier.notfound"));
}
return model;
}
/**
* Method created to find a refused invoice and return the result by page
* #param param
* #param pageable
* #return Page<InvoiceRefuseModel>
* #throws ApplicationException
*/
public Page<InvoiceRefuseModel> findRefuseInvoice(InvoiceRefuseFilterParam param, Pageable pageable) throws ApplicationException {
return invoiceRefuseDao.findRefuseInvoice(param, pageable);
}
/**
* Method created to find a refused invoice and return the result by list
* #param param
* #return List<InvoiceRefuseModel>
* #throws ApplicationException
*/
public List<InvoiceRefuseModel> findRefuseInvoice(InvoiceRefuseFilterParam param) throws ApplicationException {
return invoiceRefuseDao.findRefuseInvoice(param);
}
/**
* Method created to find a refused invoice by order number and return the result by list
* #param nuOrder
* #return List<InvoiceRefuseModel>
*/
public List<InvoiceRefuseModel> findByNuOrder(String nuOrder){
return invoiceRefuseDao.findByNuOrder(nuOrder);
}
}

Created Chatbot on OpenFire but getting error

I am trying to make chatbot plugin for openfire.But when i run plugin on openfire then i am getting below error:-
2016.10.28 18: 04: 21 org.jivesoftware.openfire.container.PluginManager - Error loading plugin: C: \Program Files(x86)\ Openfire\ plugins\ hospitalbot
java.lang.NoSuchMethodError: org.jivesoftware.openfire.XMPPServerInfo.getName() Ljava / lang / String;
at org.jivesoftware.openfire.botz.BotzConnection.login(BotzConnection.java: 319)
at org.jivesoftware.openfire.botz.BotzConnection.login(BotzConnection.java: 272)
at org.jivesoftware.openfire.plugin.ChatBot.initializePlugin(ChatBot.java: 75)
at org.jivesoftware.openfire.container.PluginManager.loadPlugin(PluginManager.java: 447)
at org.jivesoftware.openfire.container.PluginManager.access$300(PluginManager.java: 68)
at org.jivesoftware.openfire.container.PluginManager$PluginMonitor.run(PluginManager.java: 1037)
at org.jivesoftware.openfire.container.PluginManager.installPlugin(PluginManager.java: 176)
at org.jivesoftware.openfire.admin.plugin_002dadmin_jsp._jspService(plugin_002dadmin_jsp.java: 180)
I have copied this plugin from below link :-
https://community.igniterealtime.org/docs/DOC-1130
I am getting error in below file :-
package org.jivesoftware.openfire.botz;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.SessionPacketRouter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.auth.AuthToken;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.net.VirtualConnection;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.session.LocalClientSession;
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.StringUtils;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.StreamError;
/**
* The objective of BotzConnection class is to create a robot/bot application as
* an internal user of the main XMPP server. The class's login methods performs
* the necessary (virtual) connection to the server. The bot can login as an
* anonymous or a real user.
*
* <p>
* The class's object uses a BotzPacketReceiver object passed to it via one of
* it's constructors or via calls to
* {#link #setPacketReceiver(BotzPacketReceiver)} method to receive packets from
* other XMPP entities to the bot. The bot can reply to these packets with
* {#link #sendPacket(Packet)} method. Thus, a class that wants to handle bot
* packets must implement {#link BotzPacketReceiver} class.
*
* <p>
* Below is a sample parrot bot code snippet illustrating how to use
* BotzConnection and BotzPacketReceiver:
*
* <blockquote>
*
* <pre>
* *
* * BotzPacketReceiver packetReceiver = new BotzPacketReceiver() {
* * BotzConnection bot;
* * public void initialize(BotzConnection bot) { this.bot = bot; }
* * public void processIncoming(Packet packet) {
* * if (packet instanceof Message) {
* * packet.setTo(packet.getFrom());
* * bot.sendPacket(packet);
* * }
* * }
* * public void processIncomingRaw(String rawText) {};
* * public void terminate() {};
* * };
* *
* * BotzConnection bot = new BotzConnection(packetReceiver);
* * try {
* * bot.login("MyUsername");
* * Presence presence = new Presence();
* * presence.setStatus("Online");
* * bot.sendPacket(presence);
* * } catch (Exception e) {
* * }
* *
* </pre>
*
* </blockquote>
*
* #author Aznidin Zainuddin
* #see BotzPacketReceiver
*/
public class BotzConnection extends VirtualConnection {
/**
* The packet receiver object that will handle receiving of packets.
*/
private BotzPacketReceiver packetReceiver;
/**
* Holds the initialization state of the packet receiver.
*/
private boolean initPacketReceiver;
/**
* Holds the session for the bot.
*/
private LocalClientSession localClientSession;
private Roster roster;
private JID jid;
/**
* Creates a new instance of BotzConnection.
*/
public BotzConnection() {}
/**
* Creates a new instance of BotzConnection with the specified packet
* receiver.
*
* <p>
* When login is attempted with an instance created with this constructor,
* the packetReceiver traps incoming packets and texts as soon as the bot
* logs on.
*
* #param packetReceiver
* BotzConnection packetReceiver
*/
public BotzConnection(BotzPacketReceiver packetReceiver) {
this.packetReceiver = packetReceiver;
}
public LocalClientSession getLocalClientSession() {
return localClientSession;
}
public Roster getRoster() {
return roster;
}
/**
* The method will be implicitly called by the server when the bot's
* connection is (virtually) closed. The method terminates the packet
* receiver.
*/
#
Override
public void closeVirtualConnection() {
if (packetReceiver != null && initPacketReceiver) {
packetReceiver.terminate();
initPacketReceiver = false;
}
}
/**
* Calls to this method is made by the server to deliver packets to the bot.
* This method will in turn call
* {#link BotzPacketReceiver#processIncoming(Packet)} of the packet receiver
* associated with the bot.
*
* #param packet
* XMPP packet
* #throws UnauthorizedException
* When packets could not be delivered due to authorization
* problem.
*/
public void deliver(Packet packet) throws UnauthorizedException {
if (packetReceiver == null)
return;
packetReceiver.processIncoming(packet);
}
/**
* Calls to this method is made by the server to deliver raw text to the
* bot. This method will in turn call
* {#link BotzPacketReceiver#processIncomingRaw(String)} of the packet
* receiver associated with the bot.
*
* #param text
* The text string delivered to the bot.
*/
public void deliverRawText(String text) {
if (packetReceiver == null)
return;
packetReceiver.processIncomingRaw(text);
}
/*
* (non-Javadoc)
*
* #see org.jivesoftware.openfire.Connection#getAddress()
*/
//#Override
public byte[] getAddress() throws UnknownHostException {
return InetAddress.getLocalHost().getAddress();
}
/*
* (non-Javadoc)
*
* #see org.jivesoftware.openfire.Connection#getHostAddress()
*/
//#Override
public String getHostAddress() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
/*
* (non-Javadoc)
*
* #see org.jivesoftware.openfire.Connection#getHostName()
*/
//#Override
public String getHostName() throws UnknownHostException {
return InetAddress.getLocalHost().getHostName();
}
/**
* Get the bot's packet receiver
*
* #return BotzPacketReceiver packetReceiver
*/
public BotzPacketReceiver getPacketReceiver() {
return packetReceiver;
}
/**
* Get the resource portion of the bot's JID.
*
* #return Resource portion of the bot's JID.
*/
public String getResource() {
if (localClientSession == null)
return null;
return localClientSession.getAddress().getResource();
}
/**
* Get the node's portion of the bot's JID.
*
* #return Node portion of the bot's JID.
*/
public String getUsername() {
if (localClientSession == null)
return null;
return localClientSession.getAddress().getNode();
}
/**
* Get the node's portion of the bot's JID.
*
* #return Node portion of the bot's JID.
*/
public JID getIdentity() {
if (localClientSession == null)
return null;
return localClientSession.getAddress();
}
/**
* Check whether the bot session is still active.
*
* #return <tt>true</tt> if the bot is still active, <tt>false</tt>
* otherwise.
*/
public boolean isLoggedOn() {
return !isClosed();
}
/**
* Login to the XMPP server as an anonymous user. This method creates a
* virtual connection to the XMPP server and establish a user session. If
* the packet receiver is already defined, initialize it.
*
* #throws BotzSessionAlreadyExistsException
* If the users session already exists.
*/
public void login() throws BotzSessionAlreadyExistsException {
if (isClosed())
throw new BotzSessionAlreadyExistsException();
localClientSession = (LocalClientSession) SessionManager.getInstance().getSession(jid);
localClientSession.setAnonymousAuth();
if (packetReceiver != null) {
packetReceiver.initialize(this);
initPacketReceiver = true;
}
return;
}
/**
* A convenient way to login. It uses the default "Botz" as the JID resource
* and auto create the user if it doesn't exist.
*
* #param username
* The username to login with.
* #throws BotzSessionAlreadyExistsException
* If the bot's session already exists.
* #throws UserNotFoundException
* If it fails to create the user.
*
* #see #login(String, String, boolean)
*/
public void login(String username)
throws BotzSessionAlreadyExistsException, UserNotFoundException {
login(username, "Botz", true);
}
/**
* A convenient way to login. It auto create the user if it doesn't exist.
*
* #param username
* The username to login with.
* #param resource
* The resource the user will bind to.
* #throws BotzSessionAlreadyExistsException
* If the bot's session already exists.
* #throws UserNotFoundException
* If it fails to create the user.
*
* #see #login(String, String, boolean)
*/
public void login(String username, String resource)
throws BotzSessionAlreadyExistsException, UserNotFoundException {
login(username, resource, true);
}
/**
* Login to the XMPP server and establish a non-anonymous user session using
* the given username and resource. When <tt>createIfNotExist</tt> is
* <tt>true</tt>, a new user with the username will be created and stored
* in the database if it does not exist. When <tt>false</tt>, and the
* user does not exist, the method will not attempt the login. Whenever
* there's an error, the bot will not login.
*
* #param username
* Username to login with.
* #param resource
* The resource the user will bind to.
* #param createIfNotExist
* When specified as <tt>true</tt>, a new user will be created
* and stored in the database if it does not exist.
* #throws BotzSessionAlreadyExistsException
* If the bot's session already exists.
* #throws UserNotFoundException
* If it fails to create the user.
*/
public void login(String username, String resource, boolean createIfNotExist)
throws BotzSessionAlreadyExistsException, UserNotFoundException {
if (isClosed())
throw new BotzSessionAlreadyExistsException();
jid = new JID(username.toLowerCase(), XMPPServer.getInstance().getServerInfo().getXMPPDomain(), resource);
ClientSession oldSession = XMPPServer.getInstance().getRoutingTable()
.getClientRoute(jid);
// Check for session conflict
if (oldSession != null) {
try {
oldSession.incrementConflictCount();
int conflictLimit = SessionManager.getInstance()
.getConflictKickLimit();
if (conflictLimit != SessionManager.NEVER_KICK) {
// Kick out the old connection that is conflicting with the
// new one
StreamError error = new StreamError(
StreamError.Condition.conflict);
oldSession.deliverRawText(error.toXML());
oldSession.close();
} else
throw new BotzSessionAlreadyExistsException();
} catch (Exception e) {
Log.error("Error during login", e);
}
}
if (!XMPPServer.getInstance().getUserManager().isRegisteredUser(
jid.getNode())) {
if (createIfNotExist) {
try {
// Bot doesn't care of whatever password it is.
XMPPServer.getInstance().getUserManager().createUser(
jid.getNode(), StringUtils.randomString(15), null,
null);
} catch (UserAlreadyExistsException e) {
// Ignore
}
} else {
throw new UserNotFoundException();
}
}
localClientSession = (LocalClientSession) SessionManager.getInstance().getSession(jid);
localClientSession.setAuthToken(new AuthToken(jid.getNode()), jid
.getResource());
if (packetReceiver != null) {
packetReceiver.initialize(this);
initPacketReceiver = true;
}
this.roster = XMPPServer.getInstance().getRosterManager().getRoster(username);
}
/**
* Logout the bot and destroy the active session. This method need not be
* called explicitly unless, for example, when callers need to refresh the
* assign a different username or resource (re-login).
*/
public void logout() {
close();
}
/**
* Send a packet out to an XMPP entity. The packet must be one of
* <message/>, <iq/> or <presence/>. Callers need not specify the
* <tt>from</tt> attribute inside the packet because it will be
* automatically inserted with/replaced by the bot's real JID.
*
* #param packet
* The packet to send.
*/
public void sendPacket(Packet packet) {
if (isClosed())
throw new IllegalStateException("No valid session");
SessionPacketRouter router = new SessionPacketRouter(localClientSession);
router.route(packet);
}
/**
* Assign a packet receiver ({#link BotzPacketReceiver}) object that will
* receive packets to the bot. The method can be called repeatedly if
* necessary to dynamically change different packet receivers during a
* login. If the previous packet receiver is in an initialized state during
* this call, it will be terminated; and the new packet receiver will be
* initialized.
*
* <p>
* If the previous packetReceiver is the same with the new one, this method
* will ignore the assignment.
*
* #param packetReceiver
* The packetReceiver object
*/
public void setPacketReceiver(BotzPacketReceiver packetReceiver) {
if (this.packetReceiver == packetReceiver)
return;
if (this.packetReceiver != null && initPacketReceiver) {
this.packetReceiver.terminate();
initPacketReceiver = false;
}
this.packetReceiver = packetReceiver;
if (!isClosed()) {
this.packetReceiver.initialize(this);
initPacketReceiver = true;
}
}
/**
* Calls to this method is made by the server to notify about server
* shutdown to the bot.
*/
public void systemShutdown() {
close();
}
#
Override
public ConnectionConfiguration getConfiguration() {
// TODO Auto-generated method stub
return null;
}
}
Can any one please help me out as i am newb in openfire.
I think bot has not been customized for newer versions as you can see in trailing last posts in that discussions that it doesn't work for anyone.But you can always use smack api to create your own bot.
Create a bot id.
Create a Java App and connect to Openfire using Smack
Use a business logic once you receive the chats and reply to the same.

Eclipse custom perspective changed approach Kepler to Mars

I am updating an existing working RCP 3 app from
Kepler to Mars. It was written by another guy so having to learn a lot about RCP as I go.
What worked in Kepler was this:
public class ShowSelectViewDialogHandler extends DialogHandler {
/**
* The name of the parameter providing the view identifier.
*/
private static final String VIEW_ID_COMMAND_PARAMETER_NAME = "org.eclipse.ui.views.showView.viewId"; //$NON-NLS-1$
private static final String MAKE_FAST_COMMAND_PARAMETER_NAME = "org.eclipse.ui.views.showView.makeFast"; //$NON-NLS-1$
private final IHandler handler;
/**
* Creates a new ShowViewHandler that will open the view in its default location.
*/
public ShowSelectViewDialogHandler (final IHandler handler) {
this.handler = handler;
}
#Override
public final Object execute(final ExecutionEvent event) throws ExecutionException {
Object result = null;
IWorkbenchWindow window = EDMUIApplication.instance().getWorkbenchAdvisor().getWorkbenchWindowAdvisor().getWindow();
Map<String, String> parameters = event.getParameters();
String viewId = parameters.get(ShowSelectViewDialogHandler.VIEW_ID_COMMAND_PARAMETER_NAME);
String makeFast = parameters.get(ShowSelectViewDialogHandler.MAKE_FAST_COMMAND_PARAMETER_NAME);
if (viewId == null) {
ShowViewDialog dialog = new ShowViewDialog(window, new EDMUIViewRegistry(EDMUIConstants.CATEGORY_IDS));
if (dialog.open() == Window.OK) {
for (IViewDescriptor viewDescriptor : dialog.getSelection()) {
result = this.openView(window, viewDescriptor.getId(), makeFast);
}
}
} else {
result = this.openView(window, viewId, makeFast);
}
return result;
}
/**
* Opens the view with the given ID.
*
* #param window - workbench window of the view.
* #param viewId - id of the view to open.
* #param makeFast - command parameter.
* #return result of the handler execution.
* #throws ExecutionException - if default handler execution fails.
*/
private Object openView(final IWorkbenchWindow window, final String viewId, final String makeFast) throws ExecutionException {
Object result = null;
try {
Parameterization[] parameterization = this.createParameterization(viewId, makeFast, IWorkbenchCommandConstants.VIEWS_SHOW_VIEW);
result = this.executeDefaultHandler(this.handler, window, parameterization, IWorkbenchCommandConstants.VIEWS_SHOW_VIEW);
} catch (NotDefinedException ex) {
throw new ExecutionException(ex.getMessage(), ex);
}
return result;
}
/**
* Creates parameterization for the command.
*
* #param viewId - view id parameter value.
* #param makeFast - make fast parameter value.
* #param commandId - id of the command.
* #return created parameterization.
* #throws NotDefinedException - if there is no such parameter.
*/
private Parameterization[] createParameterization(final String viewId, final String makeFast, final String commandId) throws NotDefinedException {
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = commandService.getCommand(IWorkbenchCommandConstants.VIEWS_SHOW_VIEW);
IParameter viewIdParameter = command.getParameter(ShowSelectViewDialogHandler.VIEW_ID_COMMAND_PARAMETER_NAME);
IParameter makeFastParameter = command.getParameter(ShowSelectViewDialogHandler.MAKE_FAST_COMMAND_PARAMETER_NAME);
return new Parameterization[] { new Parameterization(viewIdParameter, viewId), new Parameterization(makeFastParameter, makeFast) };
}
But now ShowViewDialog signature has changed. Also the original author made the statement that his approach was based on ShowViewHandler and there must be a better more standard way of achieving the same affect, i.e. controlling the display of our reduced set of views.
Any ideas on how to achieve this? There might be a tutorial somewhere, I found the Vogella one, but it is fairly general.
ShowViewDialog is an internal dialog so it should not have been used in the first place. As you have found internal dialogs can be changed without warning.
It looks like your code is using your own implementation of IViewRegistry. To stick to using only official APIs you would have to write your own version of the show view dialog. This is a fairly simple dialog using FilteredTree and the IViewRegistry getCategories and getViews methods.
There isn't a more standard way of doing this.

Java - Pausing a thread pool when it is executing with a JButton

I have a pause mechanism for my events(Runnable) but I am not sure how to use/call it when I need it to be called.
In my class, I have a run method that submits an arraylist full of events to an executor. (see run() on GreenhouseControls). When the Events are submitted, Thread.sleep(eventTime); is called and afterwards the action() inside the event is called.
My program requirement needs a mechanism that pauses all the threads with a method(which will be called by a method when clicked) which can be resumed later on with another button.
Does anyone know how to implement this mechanism?
Here are the files:
GreenhouseControls.java
package control;
/**
* In this exercise we take a different design approach to GreenhouseControls
*
* Compiled/Tested using Eclipse Version: Luna Release (4.4.0)
* TME4 Folder is located in C:\COMP 308\
*/
import java.io.*;
import java.util.regex.*;
import java.util.logging.*;
import java.util.*;
import java.util.concurrent.*;
import java.lang.reflect.*;
import tme4.*;
/**
* GreenhouseControls consists of the Greenhouse's status and methods that
* control what actions will be performed inside the Greenhouse.
* #author Ray Masiclat
*
*/
public class GreenhouseControls extends Controller implements Serializable{
/**
* status is a Set of StatusPair objects which contains information about the
* GreenhouseControls' status. The Default States are initialized in the default constructor.
*/
private Set<StatusPair> status = new HashSet<StatusPair>();
private static String file = "src/error.log";
/**
* Default Constructor - initializes each state of the Greenhouse
*
*/
public GreenhouseControls(){
status.add(new StatusPair<String, Boolean>("light", false));
status.add(new StatusPair<String, Boolean>("water", false));
status.add(new StatusPair<String, Boolean>("fans", false));
status.add(new StatusPair<String, Boolean>("windowok", true));
status.add(new StatusPair<String, Boolean>("poweron", true));
status.add(new StatusPair<String, String>("thermostat", "Day"));
status.add(new StatusPair<String, Integer>("errorcode", 0));
status = Collections.synchronizedSet(status);
}
/**
* Prints out in the console how to use the program.
*/
public static void printUsage() {
System.out.println("Correct format: ");
System.out.println(" java GreenhouseControls -f <filename>, or");
System.out.println(" java GreenhouseControls -d dump.out");
}
/**
* Takes in an errorcode and returns the appropriate Fix that will fix the
* error that occured in the past.
* #param errorcode
* #return fix
*/
public Fixable getFixable(int errorcode){
Fixable fix = null;
switch(errorcode){
case 1:
fix = new FixWindow(this);
break;
case 2:
fix = new PowerOn(this);
break;
default:
System.out.println("No Error");
break;
}
return fix;
}
/**
* shutdown - method creates a Logger that creates an error log which consists of information about the
* reason of why the program was shut down. After logging the error information, it serializes
* the current state of the program in order for it to be fixed/restored in the future.
* #throws IOException
*/
public void shutdown() throws IOException{
System.err.println("System Shutting Down");
Logger logger = Logger.getLogger("ControllerException");
FileHandler fh;
try {
fh = new FileHandler("src/error.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Check for errorcode using getError method.
int checkError = this.getError();
if(checkError == 1){
logger.info("Window Malfunction");
} else if (checkError == 2){
logger.info("Power Outage");
} else {
logger.info("No Error");
}
/**
* Serialize the current state and output it onto the src/ directory as dump.out
*/
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/dump.out"));
out.writeObject(this);
out.writeObject(getEventList());
out.close();
System.exit(0);
}
/**
* getStatus - returns the status Set which consists of information about the current state of the program.
* #return status
*/
public Set<StatusPair> getStatus(){
return status;
}
/**
* getError - Assigns an integer which will be used when logging the error.
* The errorNum is retrieved using the status Set
* #return errorNum
*/
public int getError(){
int errorNum = 0;
//Iterate through the status Set
for(StatusPair sp : status){
//Check for the name "errorcode" and initializes errorNum from its status
if(sp.getName().equals("errorcode")){
errorNum = (int) sp.getStatus();
}
}
return errorNum;
}
/**
* getEventList - returns a List of Events in the program.
* #return eventList
*/
public List<Event> getEventList(){
return eventList;
}
/**
* Create a method in GreenhouseControls called setVariable to
* handle updating to this collection. Use the synchronization feature
* in java to ensure that two Event classes are not trying to add to
* the structure at the same time.
* s - represents the name of the status
* o - represents the status it is going to be replaced with
* #param s
* #param o
*/
public void setVariable(String s, Object o){
for(StatusPair sp : getStatus()){
if(sp.getName().equals(s))
{
sp.setStatus(o);
}
}
}
/**
* addStatus - Adds a new status in the status Set and it is used
* if the program cannot find a name in the status Set.
* #param name
* #param status
*/
public void addStatus(String name, Object status){
getStatus().add(new StatusPair<String, Object>(name,status));
}
/**
* run - creates a thread array which will be used to run Events from
* the text file. Then a for loop is created to fill up the thread
* array with Events on each index. Then the thread is started once
* an index is initialized. After an event is added in to the thread
* array it is removed from the events list.
*/
public void run(){
ExecutorService exec = Executors.newCachedThreadPool();
if(eventList.size() == 1){
exec.submit(eventList.get(0));
eventList.remove(eventList.get(0));
} else {
for(Event e : eventList){
exec.submit(e);
}
}
exec.shutdown();
/*while(eventList.size() > 0){
for(int i = 0; i < eventList.size(); i++){
exec.submit(eventList.get(i));
}
}*/
/*Thread[] threads = new Thread[eventList.size()];
while(eventList.size() > 0)
for(int i = 0; i < eventList.size(); i++){
threads[i] = new Thread(eventList.get(i));
threads[i].start();
eventList.remove(i);
}*/
}
} ///:~
Event.java
/**
* Make Event implements Runnable so that each type of event provides
* its own timing. Each event file should be a class of its own. Change
* the rest of the design to simplify this model.
*
* Assignment: TME4
* #author Ray Masiclat
* #studentid 3231308
* #date July 27, 2015
*
*
* Compiled/Tested using Eclipse Version: Luna Release (4.4.0)
* TME4 Folder is located in C:\COMP 308\
*/
package tme4;
import java.io.*;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import gui.*;
import control.*;
/**
* Event class that implements Runnable and Serializable
* #author Ray Masiclat
*
*/
public abstract class Event implements Runnable, Serializable{
/**
* eventTime - represents time it takes for the program to wait and then
* call its action method.
*/
protected long eventTime;
protected GreenhouseControls gcontrol;
protected boolean suspended = false;
/**
* Event class constructor which is used to initialize the Event's eventTime and
* GreenhouseControls object which is used for the Event to have access to its status
* variables.
* #param gc
* #param eventTime
*/
public Event(GreenhouseControls gc,long eventTime){
this.eventTime = eventTime;
this.gcontrol = gc;
}
/**
* getTime - returns the event's eventTime initialized from the constructor.
* #return eventTime
*/
public long getTime(){
return eventTime;
}
/**
* setTime - sets the eventTime
*/
public void setTime(long eventTime){
this.eventTime = eventTime;
}
/**
* run - Event class' run method is called when the Event is added in to the Thread
* and then "started". This method puts the Thread to sleep for however long the
* eventTime is. Afterwards, once it is done, it tries to run the Event's action
* method. If the action method throws an error, it is caught by the try-catch
* block which calls the GreenhouseControls object's shutdown which outputs an error.log
* file and serializes the current state of the GreenhouseControls object to a dump.out file.
*/
public void run(){
try {
synchronized(this){
while(suspended){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Thread.sleep(eventTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.action();
} catch (ControllerException e) {
//Use shutdown to create error log
try {
gcontrol.shutdown();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
public void suspend(){
suspended = true;
}
public synchronized void resume(){
suspended = false;
notify();
}
/**
* abstract method used to call an Event's action method in the run method.
* #throws ControllerException
*/
public abstract void action() throws ControllerException;
} ///:~
(Pastebin: GreenhouseControls.java
and Event.java)
Something like this:
boolean stop = false;
You can use a suspending/stopping object, like so
public class Stopping {
public synchronized void checkStop() {
if (stopped) {
wait(); // wait until stop has been cleared
}
}
public synchronized void setStop(boolean val) {
stop = val;
if (val) {
notifyAll();
}
}
}
Create one of these and share it with all your threads, making sure to call checkStop() frequently. Alternatively, you could make the methods in this class static, and then your threads wouldn't need an object reference - they could just call Stopping.checkStop();

Categories

Resources