java


Compile java code at runtime


I have a java class that is used to perform login action using selenium. There are currently 10+ different login types and as such there is a lot of if else involved which looks bad and is not efficient.
Eg:
if (logintype == 1 )
{
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("signin")).click();
}
else if (logintype ==2 )
{
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("signin")).click();
}
...........
...........
Other than code not being efficient the new code needs to be written, pushed and the server needs to be restarted every time a new login module is added.
I wanted to see if i can get the logic for login can be stored in db and if it can be compiled at runtime. I found groovy shell but i dont know how to get the results back to my class file. Also running groovy shell would require a lot of code changes. Is it possible in java
public class ExecuteAuth implements Runnable{
private WebDriver driver;
driver = new FirefoxDriver(firefoxBinary, profile, cap);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
//MongoDB code
DBCursor dbObjects = loginCollection.find();
while (dbObjects.hasNext()) {
DBObject dbObject = dbObjects.next();
loginModule.add(new LoginModule((BasicDBObject) dbObject));
String loginType = (String) dbObject.get("loginType")
String script;
if (loginType.equals("1")) {
script = (String) dbObject.get("script")
}
}
GroovyShell shell = new GroovyShell ();
shell.evaluate(script);
RUN REST OF THE LOGIN LOGIC AFTER THE CODE IS EVALUATED
}
I strongly advise against that approach. You are opening a door to bad code be injected in your application. Another way could be upload to your server your new jars and take advantage of class loader to load classes at runtime:
How should I load Jars dynamically at runtime?
Also, you have alternatives to avoid if-else's: usage of interfaces and factory methods are the way to go, imho. And put your login's implementations on different classes implementing a Login interface, for example.
Factory method design pattern:
http://www.oodesign.com/factory-method-pattern.html
http://www.javaworld.com/article/2077386/learn-java/factory-methods.html

Related Links

Draw on top of other OpenGL / DX application using Java
Cannot connect to MS Access database in Tomcat
Problem coloring the Mandelbrot set on Android
Is creating an array and an array list the same thing in Java?
Avoid VerifyError overriding a method in older APIs
How to replace these type-specific methods with a single method that uses Java Generics?
Extracting characters and integer from a string
wsimport problem with Exception in thread “main” java.lang.NullPointerException
What is the approximate per-server or per-cpu cost of WebOrb Java Enterprise? [closed]
Interactive interpreter for Java
Android: How to run asynctask from different class file?
Driver issue with PostgreSQL/Clojure
Java library to create cabinet files on Unix?
SMTPSendFailedException 590
Java Class turns invalid after a method call
Reflection api - find subclass type

Categories

HOME
compiler-construction
twitter
hive
cloud
wso2
blogger
c#-4.0
drivers
office365api
grep
xmpp
binary-tree
callback
iggrid
windows-10-universal
google-apps-marketplace
apache-cayenne
django-simple-history
tostring
jsdoc
aurigma
pythonanywhere
physics-engine
accessor
captiveportal
jndi
blazemeter
one-to-many
applozic
maquette
atlassian-crucible
libssl
flink-streaming
catel
y86
overriding
document.write
scrollable
knpmenubundle
crosswalk-runtime
or-tools
mediaelement
background-service
deadbolt-2
ado.net-entity-data-model
underscore.js-templating
trash
paypal-express
two-factor-authentication
associative-array
reactive-banana
angular-leaflet-directive
deis
sailfish-os
bigbluebutton
kineticjs
formatjs
jscript.net
codeigniter-url
asp.net-dynamic-data
java.util.concurrent
html-helper
rdl
xojo
google-reader
dml
jboss-weld
transcoding
flexmojos
tidy
google-cloud-save
tws
dataservice
mqx
smtp-auth
android-hardware
cascalog
ticoredatasync
chrono
osql
spring-portlet-mvc
krl
audio-capture
nerddinner
spec#
defensive-programming
msdev
data-acquisition

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App