In a java project i need to call (lets say generic, utility) web services. For instance giving a city code as parameter and getting details about that city. Web services are already implemented and i can only consume them.
I had the same situation before in another project and created a class for that kind of webservices. That class had several web service call methods and all were static methods.
Now, i dont really want to do same thing again because i dont think thats the right way to do it (hard to debug etc). I also dont want to make a different class for all these methods and make an instance for each call because they are too generic and instantiation seems like an overhead for that situation.
So, alternatives comes to my mind is
Using old method. One static class, several methods.
Singleton class. Most probably will have syncronisation problems, so will have overhead using locking mechanisms.
Both are not the best solutions what would you suggest?
Thanks in advance.
Related
Coming from Java EE to an Android application (i'm not Guru in Java EE), For a fatest load of the application, we decided to process a files asynchronously, when the application starts, we launch some asynchronous channels to read the files, then we open the main activity, the idea is the user may not go directly to the use cases which need these files, but if he go it direclty and the asynchronous read wasn't terminated, he should wait a while(what the client wanted).
My question is how to passe the Future object between activities? my team agreed for using static methods or singletons, but i really don't love singleton because its an antipattern and i don't like static methods because they behave nearly like singleton.
Trying to use parceable or serializable, but future doesn't use implement anyone of it, i tried to use RoboGuice (its based on static fields but its busniss) but its a little buggy, how can i do it in android in pretty way, if there's another idea, i will be obliged to use singleton or static methods.
EventBus can be used to send objects from 1 place to another (e.g. asyncTask to Activity). It's great and easy to use, and it will accomplish what you're trying to do perfectly.
You can create your custom object classes to send between almost any 2 classes, Activities, Services, or whatever you with.
I am working on an API for a software so my users can extend it without modifying the source code. But, I want only certain functions to be accessed by certain classes for security reasons. Is there anyway to do this? Also, I have no code because I have no idea on how to do this.
Thanks! -Trent
I have two thoughts on this, one is that you can look at how Minecraft Forge created their plugin API.
Another way is to have a limited API between your core code and the actual plugins, but, you need to be careful of the platform. For example, if you write the core application in Java or C#, then I can use Aspect Oriented Programming (AOP) to bypass your security and have my code change the behavior of yours.
If you use functional programming (FP) languages, then you can protect more from this type of approach, if you also are not using languages on these platforms, but they are not perfect.
So, there is a trade-off between power and convenience, so how useful do you want your application to be, and how secure?
One possible solution that may work is if you go with something similar to Minecraft, though I doubt they do this, but, give a stub application to the user. They can extend it with plugins, and the interface functions they can modify are in the stub. When the program starts, the plugins are loaded, and the interface may be modified or extended, but, then the core program is pulled down and put into the stub, and then the actual program runs. The core program can be recompiled and manipulated so method names are changed, so reflection is harder to use, but taking this approach, and doing it well, would be hard.
BTW, I like Alex T's response, I just gave different terms to some of his, such as AOP instead of reflection and immutability is part of FP.
You mention jar, which means you are using something that runs on a JVM, so you may want to read up on AspectJ, as it can significantly alter the behavior of applications. You can have private methods, but I can put code that runs instead of yours, or change the parameters or the return value before or after the method is called.
To protect variables inside of classes, you can make them private, and accessible via getter and setter methods with varying levels of protection. This also applies to classes themselves; if you wanted to prevent the user from being able to instantiate a class, you could mark the class' constructor as protected to allow instantiation only within it's package.
If you wanted to hide the implementation details of a class altogether, you could declare the class as class X instead of public class X, which would hide methods from the API for standard development.
This will quickly get you the behaviour you're after, but there's an aspect of Java called reflection, which allows an executable Java program to analyze and manipulate it's own implementation; in this regard, no field or method is ever completely safe.
You can also safeguard variables by providing access to them via 'immutable' Objects; these are objects designed to forbid the caller from modifying the original source contents.
I am starting to learn about EJB, despite I know they handle the business logic, I don't understand why an EJB has to implement an interface.
I know that the interface is a list of the methods and is used by the client to access them, but what if I don't use an interface?
I know that the no-interface view exist but when should I use an interface then?
could you please explain it using a no IT example? I am taking a course about Java EE 7 and I am stuck in this part, I have read the Oracle tutorial but I've got problems understanding this.
I apologize for my wording mistakes.
thanks in advance
The reason for an interface is because you need to invoke a method in one JVM that transparently can invoke your EJB in another JVM. All the complexity of Java EE come from that it is designed to work across multiple JVMs.
This can be handled in many ways. The approach chosen here is that an interface can make this almost transparent in your code (just compare with invocation through reflection) but the object in the first JVM contains not your code, but instead code that knows how to reach the other JVM and ask it to invoke your method and return the result.
In other words, an interface allows the compiler to help you doing it right in your code, and the application server then provides the magic glue in the object called to reach your EJB.
An EJB doesn't have to implement an interface (anymore). Well, this is only true, if you haven't different VMs accessing the same EJB container. You could host your JBoss in the cloud (e.g.) and have another JEE-Server (e.g. Tomcat) at your company site (or anywhere else) and let the TomEE retrieve its EJB instances from the JBoss Application Server. Then you have to use an interface to program against, since you don't know what the implementation will be.
Since EJB 3 no-interface views are possible. You're free to use interfaces, but you shall be happy if you don't... as long as you dont need distributed EJB-services.
An interface is always a great choice, when you design a big system though. You can easily change you underlying logic if you program against the interface and not the concrete class. So if specifications change, code is much easier to maintain.
the problem is quite simple yet maybe not resolvable?
Atleast for me :/
Situation:
Let's say I have a dynamic Page System where the Server provides additional copies of self containing GWT Modules. Meaning a main GWT instance on the client side is supposed to manage the new incoming GWT Scripts, which are simply added by the main instance itself using Tags.
Now the main GWT instance needs to communicate with the newly created script instances in the most easiest (GWT internally) way possible.
So what is out of the question: Writing stupid JSNI Wrappers on both sides, if not absolutely required.
Soltuions I came up with were:
Make a module both use, including a common interface, example:
package com.whatever.interfaces;
public interface Communication {
void showMessage(String message);
}
So both would now inherit this module and know of the definition.
The main client would now load the dynamic JS and register an implementation of Communication and the dynamic one would go and use it. I tried storing references on $wnd and on elements using setPropertyObject. On $wnd they are null, which maybe/probably related to the the GWT Iframing? For the property on the RootPanel element for example, ClassCastException would be raised.
Is there any good way to encounter this? Another idea I have is using JSNI for calling the interface as an implementation on the bridge module, but I'm not sure if this a good way.
Your help is appreciated, thanks.
EDIT:
Well I have pretty much come to the conclusion that this is not possible whatsoever.
Even though you might have used same interfaces somewhere, they will be very own instantiations of it, for different modules compiled, even if using the same module as a common ground.
The approach using JSNI certainly is somewhat possible, but not without mapping all attributes to real JS Objects and remapping them back. Meaning you can't pass complex Java Objects around like you probably would be used to. My conclusion would be, using CodeGenerators you could build the JSNI Wrappers automatically and the remappers, for both modules, but this is too much of a hassle for me.
I'm still open if someone comes up with a better idea, but I just want to provide some inside on my findings, so others can benefit of my wasted time ;)
A while ago, I created a simple prototype implementation to share Object instances to other GWT modules. You can find the code on https://code.google.com/p/gwt-plug/. And yes, as you described, it's a problem to transfer Java objects. As far as I remember, you can only transfer primitive values (int, float, ...), Strings and JavaScriptObjects. But JavaScriptObjects are a good possibility
As you already found out communicating between separately compiled GWT modules is somewhat of a challenge since everything is obfuscated. It is possible though through javascript, JSNI, JSO's and JSON.
You can use JSNI to create communication hooks through javascript directly on the $wnd object. Sort of an event bus approach would work.
You can use JSON to pass around complex objects.
You can use JSO's (JavaScript Overlays) to consume and manipulate the JSON in each of the disperate modules.
Putting all that together you would end up with a mini-framework library module that you would share between the various GWT modules you want to have communicate with each other. They would each inherit the common framework module and compile in their own obfuscated form but since they are using javascript and JSON as a common language they won't have to worry about the obfuscation.
Make sense?
As explained in the xsee's answer, you can create a hook from GWT using JSNI
In order to do this, take a look to very useful project http://code.google.com/p/gwt-exporter/
I'm about to write a MMO, using HTTP-requests that are responsed with JSON.
I was writing it all in Java EE-style, hoping it won't be hard to port to Java EE than. But then I've found out that my static instance variables for a couple of sinletons weren't created properly - classloader made a bunch of them when calling SingletonClass.getInstance() from servlets.
I was totally desperate and thought adding #Singleton descriptions would help. But things weren't so easy. My classes simply not working while adding them with #EJB ClassName var. Context lookup doesn't work either.
I was trying developing in Eclipse, NetBeans, used Glassfish, tried to set it up, but nothing really helped. I do not know what to do and really desperate now.
All I need is just few classes, that work all the time application is loaded to handle game events and hold logged users data (which is distributed in non-EJB objects that hold user data, monsters and so on), some timed events for each logged user and ability to respond to HTTP POST requests with JSON. I even do not need the ORM, I wrote all queries by myself, but still... Something that had to work simply doesn't work out.
I'm aware that all that sounds messy and non-informative, but I do not know what to do - where is my problem? Maybe, I should fill web.xml, or use different port, or fly to the moon? Or just change programming language? Sorry for your time spent reading this.
UPD. Some application scheme parts. First two from package "server".
#Startup
#Singleton
public class DbWrapper
handles all database connections, DbConnectionPool is non-singleton class, which handles pool of java.sql.Conneciton.
#Startup
#Singleton
#DependsOn("DbWrapper")
public class World
is yet another class to handle all the in-game events, that holds HashMap of logged users. User and Monster classes are from package "entities" (User holds a list of monsters).
Package "servlets" hold HttpServlet descendants, annotated #WebServlet("/pathname"), that try to use
#EJB World world
for example. But such things as world.getUser(id_user) simply won't work.
As for JDBC - postgres jar is included in GlassFish domain's /lib.
As for JSON - I use org.json found here: https://github.com/douglascrockford/JSON-java
I've found out that my static instance variables for a couple of singletons weren't created properly - classloader made a bunch of them when calling SingletonClass.getInstance() from servlets.
First, you should show us the code for one of these singleton classes. You may have made a mistake in the implementation that causes this problem.
It is true that you can get what appear to be multiple instances of a (properly implemented) singleton class in a servlet framework. But in fact they are not what they appear to be. What is actually going on is that you have loaded the class from multiple classloaders, either because you have multiple webapps each loading the class, or because you are redeploying your webapp and the previous deployment is not clearing up properly.
So what can / could you do about this?
You could use a dependency injection framework to configure your webapp, and hence avoid the need for singleton classes.
You could continue using singletons, but track down why you are getting multiple instances, and fix that problem.
You should use singletons really rarely (best would be not to use them). As an alternative use application scoped beans (#Singleton beans should normally work - they should use instance variables though, not static ones).
With Java EE 6 you also can use CDI and thus you don't have to use EJBs if you don't need the additional features they provide (like automatic transaction demarcation, security etc.) or can live with adding those features yourself.
Additionally, you can use CDI in a SE application. Keep in mind though, that you need to define the scope for CDI beans (e.g. #Application, #Request etc.) otherwise the default scope (#Dependant) is used which causes the beans to be copied on every access.