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

A whole bunch of material design errors
Write a program that prompts the user to enter a year and a month (first 3 letters, first letter uppercase) then displays the days of the month
java.net.ServerSocket does not catch all connections
How to access elements of a stream in Java 8, or return one element of a stream?
Unable to get gwt-syncproxy working
Cannot consume json with java object [duplicate]
While - try - catch in Java
Using a RandNum class to choose a Word from a Words Class, which is implemented by a word class
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'display' available as request attribute
How to tell OpenGL ES 2.0 which vertices to use?
Reading/Writing text from edittext in another application
ssl handshake exception in android gcm server
Java Application, with multiple Scenes
Netbeans JAR not running/opening properly
How to programmatically shutdown remaining mapreduce splits once job is done
Putting JSON data into a List

Categories

HOME
xamarin
magnific-popup
relative-path
iot
adb
ojdbc
adfs
slick-slider
communication
midi
size
fallback
decimal
jqwidget
crystal-reports-2008
language-agnostic
microsoft-r
opennlp
spark-jobserver
publish
windows-server-2000
webkitspeechrecognition
c++-amp
file-rename
procdump
junit5
kendo-ui-grid
file-format
bitbucket-pipelines
sqlite2
leading-zero
cookiecutter-django
fedex
gpx
azure-application-gateway
qsslsocket
theming
python-c-api
pebble-watch
lift-json
font-size
pdfclown
imanage
spring-mongodb
vtigercrm
gnome-shell-extensions
medium.com
grails-tomcat-plugin
login-control
merge-conflict-resolution
markojs
bluemix-app-scan
photobucket
ford-fulkerson
separator
cartesian-product
lemon
sframe
android-listview
ideamart
graph-drawing
operation
cisco-ios
ami
apache-commons-net
network-interface
markers
responsive-slides
heisenbug
p4java
expected-exception
comexception
poller
mbr
multiprocessor
dataadapter
qt-faststart
spring-validator
robotics-studio
simba
rdoc
propertyeditor
javaspaces
jquery-ui-layout
bigcouch
whoosh
heartbeat
android-sdk-2.1
iphone-web-app
icanhaz.js
nsviewanimation
ext3
user-friendly
compiler-specific
zend-tool
eqatec
boost-smart-ptr
django-notification

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App