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

Activity doesn't refresh layout in onCreate()
Reading default JVM property using program
What are the <> for in an ArrayList, List or other types that use <>? [duplicate]
Java 8 Stream vs Collection Storage
Adding, and multiplying 2D matrices
Drag and Drop: Trying to understand DropDemo tutorial
How to refer a jsp file in one .war location to another .war location?
Absolute path with XML parser in java
Cannot figure out constructor error [duplicate]
Trigger SWF workflow when an object gets uploaded to S3 in JAVA
Writing JPEG image in java as uncompressed still destroys data
longest substring, time limit exceeded java
how to exclude special users from security check validation in restful webservice
How do I update the latest version of parent pom?
By using servlet I'm trying to inserting the values in Mysql but it storing null values
combining 2 method calls in an if-condition

Categories

HOME
yii2
cloud
atom-editor
coq
magnific-popup
fft
session
adb
yarn
graphql
ckeditor
opengl-es-2.0
slick-slider
awesome-wm
iggrid
communication
export-to-csv
correlation
commonmark
jsprit
clearcase-ucm
windows-7-x64
karma-jasmine
visjs
icloud-api
p-value
h2db
opennlp
buildbot
dynamic-featured-image
phonegap
semantic-versioning
caret
ghost4j
xcode-extension
event-driven
android-browser
starteam
code-search-engine
twitch
nxlog
http-redirect
total-commander
qwt
logfiles
menuitem
chain-builder
atomicity
prolog-setof
pnotify
celery-task
worker-thread
kbuild
namecoin
minimization
account-kit
pycaffe
word-vba-mac
firmata
nxt
mikroc
trash
plottable.js
blackberry-10
testng-dataprovider
bgp
pyke
vhd
execute
intel-fortran
endeca-workbench
wireshark-dissector
browser-bugs
ibaction
arcanist
oxwall
javafx-webengine
rdtsc
xcode-6.2
rdl
sgen
aapt
contenttype
p4java
balanced-payments
centos5
shellexecute
free-variable
selected
quickdialog
wse3.0
bigcouch
work-stealing
gwt-ext
doh
mod-auth
tomcat-valve
telerik-scheduler
sortable-tables
brewmp
yagni
noscript

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile