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

How can I correct the rule of updating the strategic vector in the Linear Reward Inaction algorithm (LRI)
Moving backwards in a singly linked list?
Android Studio: Error of java.lang.NullPointerException when the headlessfragment called
HQL: Two conditions in where clause returning empty result
How to push reference libraries to github through eclipse?
How Convert Microsoft GUID to JAVA UUID?
Loading Customized Transformers in Apache Spark raised a NullPointerException
Parameterised Test Execution in JUnit Jupiter(JUnit5)
Linked List remove
Update JTextArea to show progress
Repeating an effect on blocks in a world
What does the leak period means in sonarQube
Spring-Security with two authentication managers
Generate unique int whenever a new object is created
How to parse the Google search result in a specific date range?
How to list both Numbers and Contact in a single row

Categories

HOME
cakephp
wso2-am
cloud
testng
relative-path
sqlite3
ravendb
ezpublish
cross-validation
medical
elasticsearch-hadoop
session-variables
gnupg
fancybox-3
quicklook
hapi
dosgi
solaris-10
lucene.net
nodatime
cx-freeze
opencover
devops
django-storage
bootstrap-material-design
filezilla
greendao
fopen
librato
gsoap
nouislider
r-raster
key-value-observing
phonegap
galsim
semantic-versioning
code-contracts
kendo-ui-grid
bytecode-manipulation
dosbox
uft-api
ws-security
preconditions
email-templates
eclipse-scout
http-live-streaming
firebase-admin
segment
long-polling
outlook-2013
osx-mavericks
qsslsocket
python-c-api
mplayer
recursive-datastructures
python-cryptography
imanage
hittest
color-profile
messenger
xcb
adxstudio-portals
firebaseui
contact-list
lttng
browser-link
srand
reactive-banana
lemon
thredds
ruby-2.2
libressl
myo
apache-commons-fileupload
jsapi
csquery
clicktag
c3
html-helper
php-5.4
mysql-error-1062
algebraic-data-types
industrial
page-layout
c18
dexterity
excel-2003
qt-faststart
ember-app-kit
free-variable
simba
qt-jambi
data-loss
pyinotify
lpeg
wise
modelstate
filtered-index
firefox-5
web-architecture
javap
ti-dsp
thread-local-storage
private-members
uiq3

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