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

Hot code swap is not working, why?
Useful Intellij Live Templates
Java Multiplication table GUI, please help me move forward
Java inline method/strategy for truncating String?
Unable to instantiate application java.lang.RuntimeException:…BackupDB cannot be cast to android.app.Application
Unable to load a saved JButton
GC overhead limit exceeded while starting eclipse after improper shutdown- GALILEO
Java webapp metrics using Spring injection
Comparing primitive type with generic type
Android - Get HTTP Post Response in other activity
JSF: Validation error
My calculator android application crashed after i added the function given below
MouseDragReleased calling MouseDragExited
İntellij idea how to deploy project with all resources
No matter what, I can't batch MySQL INSERT statements in Hibernate
Java - split string into an array

Categories

HOME
sendgrid
oracle11g
isabelle
reserved
spagobi
routes
rdf
twitter-bootstrap-4
c#-2.0
ip
echarts
webrequest
contact
spring-xd
hapi
dynamics-crm-online
opentracing
libtiff
scichart
emulator
one-hot-encoding
restful-authentication
buildbot
pepper
brunch
fifo
ping
libssl
unboundid
siesta-swift
scorm2004
framemaker
geopositioning
normal-distribution
space-complexity
unspecified
websphere-mq-fte
youcompleteme
boost-multi-index
sage-one
dropbox-php
idisposable
composite-key
dtexec
windows-mobile-6.5
background-service
azure-virtual-network
trash
xml-attribute
fputcsv
nsviewcontroller
make-install
php-parse-error
ios4
xpath-1.0
photobucket
eclipse-clp
sysinternals
oberon
angular-local-storage
pretty-print
knuth
asp.net-web-api-odata
markers
preferences
html-helper
p4java
phpthumb
reactfx
ccss
coverflow
qt-faststart
eventlistener
javaspaces
errai
viewswitcher
ocx
spring-portlet-mvc
hgsubversion
cufon
pydot
mod-auth
yetanotherforum
paster
suppress
perfect-hash
compiler-specific
text-coloring
lzh

Resources

Encrypt Message