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

Using JavaRDD.flatmap() iterator in Apache Spark 2
Java GUI Table adding certain rows to a watch-list
Complex string split method in java
Using Recursion to determine if input is prime
TensorFlow: initializing state for RNN between inferences in Android
How to store the full HTML table in JTable after parsing with JSoup?
What is a test script?
java regex matching &[text(text - text text) !text]
Container Managed EntityManager is Null on REST Application
Spring Mail, Failure sending HELO comand to SMTP Server
Postman returns status 500 regardless of which JEE exeption do I use
Why is impurity measure is not a good measure if there are three or more possible classes that the decision tree must distinguish?
JSTL error: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
Volatile keyword in java under the hood
Add TableViewer Data in preference Store + eclipse
Gobblin MR Job throws Invalid path ' ' Exception

Categories

HOME
arduino-uno
netbeans
react-redux
microservices
fsm
umd
webpack-2
alpha
blueprintjs
windows-azure-storage
gz
commonmark
zebra-printers
circuit
google-static-maps
firefox-webextensions
reverse-proxy
java-3d
cloudhub
graphicsmagick
wijmo
smb
facet
social-media
wtx
kendo-datasource
key-value-observing
exuberant-ctags
phpfreechat
textmate
form-data
y86
overriding
io-redirection
file-format
retina-display
elasticsearch-plugin
azure-ml
galen
thin
qwt
darcs
crosswalk-runtime
spring-android
radtreelist
paypal-express
bstr
contact-list
prerequisites
cartesian-product
teamcity-8.0
sframe
microbenchmark
twirl
apache-commons-fileupload
unity3d-gui
gadt
tt-news
angularjs-ng-click
neolane
bitcoinj
fluentautomation
braille
jboss-weld
didselectrowatindexpath
meteor-velocity
industrial
uv-mapping
ms-project-server-2010
gridfs
tws
bulkloader
flash-builder4.5
eventual-consistency
ember-app-kit
specification-pattern
pvrtc
hamiltonian-cycle
code-cleanup
nsdatecomponents
django-tagging
lang
w3wp.exe
h.323
asp.net-profiles
bespin
perfect-hash
multiple-languages

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