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

Insert instead of update with Spring Data JPA
Text Fields will not populate
Thread creation error when using the useLibrary clause in gradle
Java EE authentication: how to capture login event?
Add qualified class to Java comment instead of importing the class
Got 404 error on tomcat 7.0.47 websocket
Maven Java Project Only Working Once
drag object using mouse Listener
How to remove 2 or more spaces that are next to each other from a char array
Java int array incremented with start and end points
Another Comparison method that violates its contract [duplicate]
FTP proxy design: how to shutdown data channel properly at client side
NullPointerException at mapFragment.getMapAsync
Cannot create ActiveMQ queue or send a message using java
Type of constant expressions in Java [duplicate]
Hazelcast MapStore Load with TTL

Categories

HOME
vbscript
cookies
heroku
reserved
view
vmware
setup-deployment
paw-app
icloud
convolution
django-imagekit
here-api
spring-xd
ef-migrations
undefined
zapier
errorlevel
language-agnostic
crosstab
android-widget
javacv
su
publish
typo3-6.2.x
msys2
kendo-datasource
quote
unboundid
nameservers
normal-distribution
environment-modules
catalog
host
appfabric
main
uft-api
jna
eclipse-scout
jvm-languages
bootstrap-dialog
jquery-nestable
clean-architecture
blogengine.net
return-value
mu
tactic
cudafy.net
arrow-keys
minimization
quartz-composer
cubes
heidisql
adxstudio-portals
underscore.js-templating
pcf
connect-by
holder.js
ready-api
design-by-contract
phpcas
srand
wordml
libressl
security-testing
android-listview
iiviewdeckcontroller
composite
streambase
device-orientation
modalpopup
operator-precedence
tween
pick
google-reader
uitouch
marmalade
algebraic-data-types
typo3-neos
geos
runtime.exec
apc
android-screen-support
wsdl-2.0
google-email-migration
dsn
multipage
ocx
enter
dotnethighcharts
subscript
spyware
zpt
horizontal-accordion
appendto
calling-convention
site.master
vc90
database-management
brewmp
port-number
zune
dbisam
data-acquisition





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