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

basic java for mobile development
Java (Eclipse) - Conditional compilation
“duplicate entry” error when exporting Java project to JAR with Eclipse
TopLeft & Bottom right longitude and lattitude value of MapView in Android SDK
Unable to run compiled .classes from the command line
Mapping vs composition
tomcat relative paths settings
Java - Throttling SOAP API calls
Java filename expansion library
what's the use of a frame , a pane or a panel in swing?
Newbie using Google App Engine
Any sites/books/tricks to practice recursion and OO design? [closed]
Thread.sleep() with synchronization in java
ADB - how does it work?
Implementation of Atomic wait in Java
Need enough information about stateless session in hibernate

Categories

HOME
vim
json.net
fft
vmware
microservices
google-project-tango
django-imagekit
metatrader4
ssl-client-authentication
kentor-authservices
quicklook
leiningen
flask-wtforms
http-status-code-504
text-rendering
amazonsellercentral
visjs
opencover
orleans
dcevm
tdd
typo3-6.2.x
binary-data
bosh
preg-match-all
data-manipulation
caret
wallpaper
google-api-nodejs-client
overriding
suricata
file-format
temporary-files
elmah
node-sass
typescript1.8
fancybox-2
togetherjs
brightcove
namecoin
qtwebview
pillow
linode
yt-project
ubuntu-10.04
whois
yaws
pundit
service-accounts
method-parameters
composite
notify
rake-task
gwidgets
rdtsc
dealloc
quartz-graphics
ftps
easy-install
ora-00911
django-nonrel
chronoforms
orchardcms-1.7
selected
pvrtc
path-separator
dbproviderfactories
qtkit
netbeans-6.9
assembly-loading
qt-jambi
psi
xmlspy
table-footer
telerik-scheduler
ext3
bespin
w3wp

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