is it possible to use togglz in non web application - java

I am trying to find if it is possible to use togglz in non web application - like we have plain java project or java batch programs.
I tried adding the togglz library in the stand alone application and tried running it.
this is my code snippet -
import com.feature.MyFeature;
public class Test {
public static void main(String[] args) {
Test t = new Test();
boolean valid=t.validate("CREATE_TEAM");
System.out.println(valid);
}
public boolean validate(String feature){
if (MyFeature.valueOf(feature).isActive()) {
return true;
}
return false;
}
}
It says -
Exception in thread "main" java.lang.IllegalStateException: Could not find the FeatureManager. For web applications please verify that the TogglzFilter starts up correctly. In other deployment scenarios you will typically have to implement a FeatureManagerProvider as described in the 'Advanced Configuration' chapter of the documentation.
at com.amdocs.switchlite.core.context.FeatureContext.getFeatureManager(FeatureContext.java:53)
at com.feature.MyFeature.isActive(MyFeature.java:20)
at Test.validate(Test.java:22)
at Test.main(Test.java:12)

You will have to configure Togglz correctly to make it work. In a standalone application I recommend the following setup.
First create a FeatureManager using the FeatureManagerBuilder. Something like this:
FeatureManager featureManager = FeatureManagerBuilder.begin()
.featureEnum(Features.class)
.stateRepository(new InMemoryStateRepository())
.userProvider(new NoOpUserProvider())
.build();
The tell StaticFeatureManagerProvider about your manager:
StaticFeatureManagerProvider.setFeatureManager(featureManager);
Now StaticFeatureManagerProvider is able to tell Togglz about your FeatureManager and everything should work fine!
Features.FOOBAR.isActive();
// > false

Related

SonarQube rule: "Using command line arguments is security-sensitive" in Spring Boot application

SonarQube is just showing a Critical security issue in the very basic Spring Boot application. In the main method.
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
SonarQube wants me to Make sure that command line arguments are used safely here.
I searched this on both StackOverflow and Google, and I am surprised that I couldn't find any single comment about this issue. I am almost sure that there are some security checks inside the SpringApplication.run method already. And also, I don't even remember that anyone sanitizes the main method arguments before calling SpringApplication.run. I simply want to tag it as false positive and move on.
Part of this question is also asked here: SonarQube shows a secuirty error in Spring Framework controllers and in Spring Framework Application main class
Is it false positive?
If you are not using any command-line arguments ,then you could avoid mentioning the args parameter in the run method .Like the below code.
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
This will remove sonarqube hotspot issue.
If you are sure then you can include the following to get rid of the issue.
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
It appears this is marked as a security hotspot as per sonar documentation. It states
Unlike Vulnerabilities, Security Hotspots aren't necessarily issues that are open to attack. Instead, Security Hotspots highlight security-sensitive pieces of code that need to be manually reviewed. Upon review, you'll either find a Vulnerability that needs to be fixed or that there is no threat.
You can read more about it here security hotspot
As per this rule RSPEC-4823 or S4823, command line arguments are to be evaluated based on
Any of the command line arguments are used without being sanitised
first.
Your application accepts sensitive information via command line
arguments.
If your application falls into this category they are definitely a possible security issue to your application.
No, it is a critical security issue indeed. It's just asking to sanitize the args before using it. There's no need for such a concern on a simple application, but it may be a big matter on a production application.
More details can be found on https://rules.sonarsource.com/java/RSPEC-4823?search=Make%20sure%20that%20command%20line%20arguments%20are%20used%20safely%20here.
do not use command line paramters can fix this issue.
only for sonar check change code
from:
SpringApplication.run(ApiDispatchApplication.class, args);
to:
SpringApplication.run(ApiDispatchApplication.class);

Does Maven offer hooks at the very beginning of runtime

Say we are running mvn test.
I am wondering if there is a way to configure Maven to run some files before executing tests. In my case, I want to configure a library, but don't want to have to configure this library for every entrypoint in my app/tests. I am just looking to configure the lib for every mvn lifecycle hook which invokes a runtime.
Something like this:
#MavenRuntimeLifecycle
public class Whatever {
public void runtimeBegin(){
// right when the java process starts up
Mylib.configure("foo");
}
public void runtimeEnd(){
// right before the process shuts down
}
}
I assume this would be a Maven specific thing - not that it has to be in the same Java process as my server or tests etc.
Note that using Node.js, I would simply do it like so:
export class MyLib {
isConfigLoaded = false;
static loadConfig(){
// ...
}
static void run(){
if(!this.isConfigLoaded){
MyLib.loadConfig(require('../some/path/to/.mylib.config.js'));
this.isConfigLoaded = true;
}
this.doTheThing();
}
}
I could do the same thing with Java or Maven project, and just store a .java file in the resources directory. It's more manual, but it could be done.

Load testing framework to test java applications

Is there a load testing framework that I could use where I can supply my own Java class and test the performance of that class. So basically the framework would essentially spawn threads and record when those threads finished running and then generate a report with the final results.
Apache JMeter is exactly the project you want. You can point it at a running process or have it spin up multiple threads each starting a process. It will monitor the throughput, error rate and anything else you are interested in and render it all in a set of charts.
Take a look into Metrics (http://metrics.codahale.com/). You can use it to instrument your app, and get interesting reports after a test suite run or even published to a metrics server.
Assuming you have a Java Class and a Test method like below:
import org.junit.Test;
public class AnyTestEndPoint {
#Test
public void anyTestMethod() throws Exception {
...
your code goes here for a single user
...
}
}
Your above test can be fed to the load generator with following configs.
You can spawn virtual-users from a simple properties config file like below.
# my_load_config.properties
#############################
number.of.threads=50
ramp.up.period.in.seconds=10
loop.count=1
In the above config, number.of.threads represents virtual users to be ramped up concurrently.
Then your load test looks like below which is pointing to the above Test:
#LoadWith("my_load_config.properties")
#TestMapping(testClass = AnyTestEndPoint.class, testMethod = "anyTestMethod")
#RunWith(ZeroCodeLoadRunner.class)
public class LoadTest {
}
This can be achieved for JUnit4 load generation and JUnit5 load generation. See the running examples in the HelloWorld GitHub repo.
You could try JUnit or TestNG. I have used them in the past. Not sure if it exactly what you are looking for.

akka with play framework

still learning to master akka java with play framework. I have a code snippet below. It was working fine but has decided to give some headaches.
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null));
........ somecode
}
it was working fine but now my eclipse juno complains that it cannot resolve the index object in the return line . I am new to both akka and play framework . Can someone please explain what is happening to me. cos have to submit the project as my final year project. thanks
Your problem is not related to Akka, it's a template concern.
The variable index is provided by a template import, certainly import views.html.*;
Eclipse sometimes cannot resolve this object because it is generated automatically by Play after the first request.
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
See the hello word sample for a concrete exemple.

Eclipse JSP preview

Is there an Eclipse plugin or feature that allows previewing of JSP files? Ideally such a feature would be aware of Spring tags. It's a major pain to edit the JSP in Eclipse, then build and deploy to see the results.
I haven't seen any good plugin which will satisfy your requirement.
As an alternative you can put the jetty server's jar to your class path (I am using jetty-6.1.5.jar and jetty-util-6.1.5.jar) and write a class like the following.
package net.eduportal.jetty;
import javax.servlet.ServletContext;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.UserRealm;
import org.mortbay.jetty.webapp.WebAppContext;
public class JettyRunner {
public static final int PORT = 8080;
public static final String BASE_URL = "http://localhost:" + PORT;
private static final JettyRunner _instance = new JettyRunner();
public static JettyRunner getInstance() {
return _instance;
}
// ///////////////////////////////////////////////////////////////
// Singleton
// /////////////
private Server server = null;
private WebAppContext wac = null;
private JettyRunner() {
}
public interface WebApplicationInitializer {
public void init(WebAppContext wac);
}
public ServletContext getServletContext() {
return wac.getServletContext();
}
public void start() throws Exception {
if (server == null) {
server = new Server(PORT);
server.setStopAtShutdown(true);
wac = new WebAppContext();
wac.setContextPath("/test");
wac.setResourceBase("war");
wac.setClassLoader(this.getClass().getClassLoader());
server.addHandler(wac);
server.start();
}
}
public void stop() throws Exception {
if (server != null) {
server.stop();
server = null;
}
}
public static void main(String argv[]) throws Exception {
JettyRunner.getInstance().start();
}
}
The above code assumes there is a folder called "war" in the class path which contains the same WEB-INF/* folders. When you run the code from eclipse the server will start and you can view the jsps by accessing the location localhost:8080/test/*
See http://jetty.mortbay.org/jetty5/tut/Server.html
You shouldn't have to rebuild at all to see the results.
The latest Enterprise version of eclipse actually does hot code replacement of JSPs. I add the web project to Tomcat (or Glassfish or JBoss...) and any change I make in a JSP is reflected after I refresh my browser window. Obviously, when I change a Java file, I need to restart Tomcat, but that only takes 2 seconds at most.
MyEclipse provides this plugin:
http://www.myeclipseide.com/module-htmlpages-display-pid-11.html
As to whether it will be Spring tag aware is another matter though...
JBoss Tools (http://jboss.org/tools) has a visual page editor that supports JSP, HTML and even JSF.
If a tag is not supported you can right click it and add a template for it OR you can extend the supported tags by implementing the extension points.
Examples of users extending the set of supported tags are http://relation.to/Bloggers/HowToCreateAVisualDocBookEditorIn10Minutes and http://planetjbpm.wordpress.com/2009/02/25/xforms-editor-with-jboss-vpe-and-some-jbpm/
There's the Oracle Workshop for WebLogic 10g R3 which gives you the closest thing to WYSIWYG JSP editing. Despite the fact that it comes from Oracle/BEA, it works with many app servers, not just WebLogic. It is the best tool I know for JSPs and it's free. I don't about Spring tags, but it can be customized to give design time representation of tags. I'm not sure if they support Eclipse 3.4 though.
There's also JBoss Developer Studio which has good JSP visual tools.

Categories

Resources