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

Weka model Read Error in android
Spring Security asking authentication issue
Access full path from JFilechooser
dynamodb query get more than 1m of data - Example in java please
Cannot find index of a character in java string
install jdk 6 or higher on a mac osx 10
Hibernate cannot see OJDBC6 driver
Dot symbol in java ant
Plugin Development, where to save user data?
send screenshots over network
Updating your UI and forcibly waiting before continuing JavaFX
Getting the Top 5 Lowest Value in An array
Spring Boot WorkManagerTaskExecutor
BigDecimal.add() being ignored
File does not seem to be renaming in Java
Can't specify class path in Java

Categories

HOME
ms-access
hive
proxy
heroku
dictionary
include
mean-stack
cvs
cmd
paw-app
icloud
yum
multiple-records
amazon-cloudformation
metatrader4
postgres-xl
dtrace
flask-wtforms
caml
dosgi
samoa
lucene.net
opencover
csrf-protection
excel-2007
crosstab
user-interaction
fgetcsv
replaceall
bpel
nat
key-value-observing
overriding
ios5
az-application-insights
unobtrusive-validation
particles.js
galen
avro4s
core-plot
http-live-streaming
mime
node-sass
wso2carbon
apache-fop
qsslsocket
dds
paxos
rdfs
statsd
sqlbulkcopy
pdfclown
gcsfuse
minimization
messenger
savon
elements
apachebench
tform
jfugue
iad
comobject
qdialog
tmuxinator
wordml
spidermonkey
kcachegrind
service-accounts
website-monitoring
c3
dd
qpainter
nsbutton
onactivityresult
fluentautomation
datagridviewcolumn
viadeo
rabl
sharp-repository
angularjs-controller
pvrtc
objective-c-2.0
libc++
rfc1123
yui-datatable
sortable-tables
.nettiers
jquery-ui-droppable
forums
perfect-hash
user-friendly
compiler-specific
web-architecture
procedural-music
mediarss
private-members
multiple-languages

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile