2. • Existing application of some complexity
• Using perhaps custom frameworks, but not yet Wonder
• Want to use Wonder for several reasons, but uncertain where to
start and how to manage the migration process
The Task
Samstag, 22. Juni 13
3. Goals
• Make the switch at a chosen, planned point in time
• Being able to switch back to the non-Wonder version if
problems come up in production
• Identify which steps to do when, and how
Samstag, 22. Juni 13
4. The Big Steps
• Move to Git (if you haven‘t already)
• Prepare the code base ahead of the actual wonderization
• Create a wonderization branch
• Wonderize in that branch, periodically merge new stuff from
your main branch
• Release and celebrate
Samstag, 22. Juni 13
5. Prep step: Move to Git
• You‘ll need branches during the migration
• Wonder is Git-based anyway
• It just makes everything easier
• Plan some time to get up to speed using Git first
• Use Sourcetree
Samstag, 22. Juni 13
6. Prep step: Move to Git
Time
release branches
masterdevelop hotfixes
feature
branches
Feature
for future
release
Tag
1.0
Major
feature for
next release
From this point on,
“next release”
means the release
after 1.0
Severe bug
fixed for
production:
hotfix 0.2
Bugfixes from
rel. branch
may be
continuously
merged back
into develop
Tag
0.1
Tag
0.2
Incorporate
bugfix in
develop
Only
bugfixes!
Start of
release
branch for
1.0
Author: Vincent Driessen
Samstag, 22. Juni 13
7. Prep step: Move to Git
Time
release branches
masterdevelop hotfixes
feature
branches
Feature
for future
release
Major
feature for
next release
Severe bug
fixed for
production:
hotfix 0.2
Tag
0.1
Tag
0.2
Incorporate
bugfix in
develop
Start ofSamstag, 22. Juni 13
8. Prep step: Move to Git
release
Tag
1.0
From this point on,
“next release”
means the release
after 1.0
Bugfixes from
rel. branch
may be
Tag
0.2
Incorporate
bugfix in
develop
Only
bugfixes!
Start of
release
branch for
1.0
Samstag, 22. Juni 13
9. Prep step: Move to Git
Tag
1.0
From this point on,
“next release”
means the release
after 1.0
Bugfixes from
rel. branch
may be
continuously
merged back
into develop
Only
bugfixes!
Start of
release
branch for
1.0
Samstag, 22. Juni 13
10. Managing Wonderization in Git
first wonderized release
second wonderized release
conventional version as fallback
conventional version as fallback
wonderized version merged into main
1.0
1.0
develop wonderize
1.1
feature
1.2
1.2
1.3
1.3
2.0
1.2
Samstag, 22. Juni 13
11. Prep step: Java packages
• You can‘t inherit from packaged classes if your classes aren‘t in
packages, too
• So if you haven‘t already, create packages and move all your
sources into them
• Benefit: clarified namespaces for your stuff
Samstag, 22. Juni 13
12. Java packages gotchas
• Getters and setters in components need to be(come) public
• Check class.getName() calls to become class.getSimpleName()
• Class.forName() needs full packaged path
• Overridden methods in enums become unreachable code in WO
bindings
Samstag, 22. Juni 13
13. public enum ContentType {
! literature {
! ! @Override public String cssClassName() { return "read"; }
! },
! film {
! ! @Override public String cssClassName() { return "watch"; }
! },
! music {
! ! @Override public String cssClassName() { return "listen"; }
! };
!
! public abstract String cssClassName();
! public boolean isAvailable() { return true; }
}
Packages: overriding enum methods
Samstag, 22. Juni 13
14. Packages: overriding enum methods
public enum ContentType {
! literature {
! ! @Override public String cssClassName() { return "read"; }
! },
! film {
! ! @Override public String cssClassName() { return "watch"; }
! },
! music {
! ! @Override public String cssClassName() { return "listen"; }
! },
! pr0n {
! ! @Override public String cssClassName() {
! ! ! return getUser().isAdult() ? "watch" : "nothingForYou";
! ! }
! };
!
! public abstract String cssClassName();
}
Samstag, 22. Juni 13
16. Packages: overriding enum methods
public enum ContentType implements NSKeyValueCoding {
! literature {
! ! @Override public String cssClassName() { return "read"; }
! },
! film {
! ! @Override public String cssClassName() { return "watch"; }
! },
! music {
! ! @Override public String cssClassName() { return "listen"; }
! },
! pr0n {
! ! @Override public String cssClassName() {
! ! ! return getUser().isAdult() ? "watch" : "nothingForYou";
! ! }
! };
! !
! public abstract String cssClassName();
! @Override public void takeValueForKey( Object obj, String s ) { return; }
! @Override public Object valueForKey( String s ) {
! ! try {
! ! ! return this.getClass().getMethod( s, (Class<?>[]) null ).invoke( this, (Object[]) null );
! ! } catch( Exception e ) {
! ! ! throw new RuntimeException( e );
! ! }
! }
}
Samstag, 22. Juni 13
17. Prep step: own EC class
• You gain a lot of flexibility by using your own EOEditingContext
subclass
• example: logging on saveChanges() or invalidateAllObjects()
• example: undoManager().removeAllActions() after saves
• Changing the superclass later to ERXEC becomes easy
Samstag, 22. Juni 13
18. Prep step: own DA class
• Create a common superclass between concrete DirectAction
classes and WODirectAction
• Changing the superclass later to ERXDirectAction becomes easy
Samstag, 22. Juni 13
19. Prep step: own logging class
• Create your own org.apache.log4j.Logger subclass
• Changing the superclass later to ERXLogger becomes easy
Samstag, 22. Juni 13
20. import org.apache.log4j.Logger;
public class MyLogger extends Logger {
! public MyLogger( String name ) {
! ! super( name );
! }
! public static Factory factory = null;
! static {
! ! String factoryClassName = MyLogger.Factory.class.getName();
! ! try {
! ! ! MyLogger.factory = (Factory) Class.forName( factoryClassName ).newInstance();
! ! } catch( Exception ex ) {
! ! ! System.err.println( "Exception while creating logger factory of class " + factoryClassName + ": " + ex );
! ! }
! }
! public static class Factory implements org.apache.log4j.spi.LoggerFactory {
! ! @Override
! ! public Logger makeNewLoggerInstance( String name ) {
! ! ! return new MyLogger( name );
! ! }
! ! public void loggingConfigurationDidChange() {
! ! }
! }
Your own logging class (1/2)
Samstag, 22. Juni 13
21. ! public static MyLogger getMyLogger( String name ) {
! ! Logger logger = MyLogger.getLogger( name );
! ! if( logger != null && ! (logger instanceof MyLogger) ) {
! ! ! throw new RuntimeException(
! ! ! ! "Can't load Logger for ""
! ! ! ! + name
! ! ! ! + "" because it is not of class MyLogger but ""
! ! ! ! + logger.getClass().getName()
! ! ! ! + "". Check if there is a "log4j.loggerFactory=er.extensions.Logger$Factory" line in your properties."
! ! ! );
! ! }
! ! return (MyLogger) logger;
! }
! public static Logger getLogger( String name ) {
! ! return Logger.getLogger( name, MyLogger.factory );
! }
! public static MyLogger getMyLogger( Class clazz ) {
! ! return MyLogger.getMyLogger( clazz.getName() );
! }
! public static Logger getLogger( Class clazz ) {
! ! return MyLogger.getMyLogger( clazz );
! }
}
Your own logging class (2/2)
Samstag, 22. Juni 13
22. Prep step: rename enums
• ERXKey constants in new templates could collide with enum
names
• Common collision pattern: uppercase enum with same name as
EO attribute
• Eclipse refactoring tools are your friend
• Make this a separate commit
Samstag, 22. Juni 13
23. public class MyFlightRoute extends _MyFlightRoute {
! public static enum STATUS {
! ! obsolete,
! ! current,
! ! preliminary,
! ! deleted;
! }
! public void setStatus( STATUS status ) {
! ! super.setStatus( status.name() );
! }
}
enum renames
Samstag, 22. Juni 13
24. public class MyFlightRoute extends _MyFlightRoute {
! public static enum STATUS {
! ! obsolete,
! ! current,
! ! preliminary,
! ! deleted;
! }
! public void setStatus( STATUS status ) {
! ! super.setStatus( status.name() );
! }
}
public abstract class _MyFlightRoute extends MyEOGenericRecord {
! public static final ERXKey<String> STATUS = new ERXKey<String>("status");
! public static final String STATUS_KEY = STATUS.key();
}
enum renames
Samstag, 22. Juni 13
25. public class MyFlightRoute extends _MyFlightRoute {
! public static enum STATUS {
! ! obsolete,
! ! current,
! ! preliminary,
! ! deleted;
! }
! public void setStatus( STATUS status ) {
! ! super.setStatus( status.name() );
! }
}
public abstract class _MyFlightRoute extends MyEOGenericRecord {
! public static final ERXKey<String> STATUS = new ERXKey<String>("status");
! public static final String STATUS_KEY = STATUS.key();
}
enum renames
Samstag, 22. Juni 13
26. public class MyFlightRoute extends _MyFlightRoute {
! public static enum FRSTATUS {
! ! obsolete,
! ! current,
! ! preliminary,
! ! deleted;
! }
! public void setStatus( FRSTATUS status ) {
! ! super.setStatus( status.name() );
! }
}
public abstract class _MyFlightRoute extends MyEOGenericRecord {
! public static final ERXKey<String> STATUS = new ERXKey<String>("status");
! public static final String STATUS_KEY = STATUS.key();
}
enum renames
Samstag, 22. Juni 13
28. Wonderization: Frameworks
• Now is the time to start the actual wonderization
• Start by the usual way to import Wonder into Eclipse
• Then add ERJars, ERExtensions,WOOgnl and Wonder‘s
JavaWOExtensions to your project
• And ERPrototypes if you want to use them
• Remove log4j and potentially other jars that are contained in
ERJars (check version compatibilities)
Samstag, 22. Juni 13
29. Properties file
• Wonder manages nearly all settings through Properties
• Live in file Resources/Properties
• You have to create at least a minimal file to start with
Samstag, 22. Juni 13
31. Application.java
• Requirement: subclass ERXApplication
• If you subclassed a custom base class instead of WOApplication,
you can either make that inherit ERXApplication, or copy the
methods you need over to your Application class.
Samstag, 22. Juni 13
32. public class Application extends ERXApplication {
! public static void main( String[] argv ) {
! ! ERXApplication.main( argv, Application.class );
! }
Application.java (1/2)
Samstag, 22. Juni 13
33. public class Application extends ERXApplication {
! public static void main( String[] argv ) {
! ! ERXApplication.main( argv, Application.class );
! }
! public Application() {
! ! WOMessage.setDefaultEncoding( "UTF-8" );
! ! ERXMessageEncoding.setDefaultEncodingForAllLanguages( "UTF-8" );
! ! // ...and whatever else you need to have here, but not more.
! ! log.info( "######### Application startup complete #########" );
! }
Application.java (1/2)
Samstag, 22. Juni 13
34. public class Application extends ERXApplication {
! public static void main( String[] argv ) {
! ! ERXApplication.main( argv, Application.class );
! }
! public Application() {
! ! WOMessage.setDefaultEncoding( "UTF-8" );
! ! ERXMessageEncoding.setDefaultEncodingForAllLanguages( "UTF-8" );
! ! // ...and whatever else you need to have here, but not more.
! ! log.info( "######### Application startup complete #########" );
! }
! // everything that can be deferred better goes here instead
! @Override public void didFinishLaunching() {
! ! new ERXShutdownHook() {
! ! ! @Override public void hook() {
! ! ! ! // cleanup that needs to run when application is shut down
! ! ! }
! ! };
! ! // example for project-specific stuff
! ! taskManager = new BackgroundTaskManager();
! ! taskManager.newRecurringTask( new MySystemState.SystemStateUpdaterTask(), 60 );
! ! super.didFinishLaunching();
! ! log.info( "######### post-startup sequence complete #########" );
! }
Application.java (1/2)
Samstag, 22. Juni 13
35. public class Application extends ERXApplication {
! public static void main( String[] argv ) {
! ! ERXApplication.main( argv, Application.class );
! }
! public Application() {
! ! WOMessage.setDefaultEncoding( "UTF-8" );
! ! ERXMessageEncoding.setDefaultEncodingForAllLanguages( "UTF-8" );
! ! // ...and whatever else you need to have here, but not more.
! ! log.info( "######### Application startup complete #########" );
! }
! // everything that can be deferred better goes here instead
! @Override public void didFinishLaunching() {
! ! new ERXShutdownHook() {
! ! ! @Override public void hook() {
! ! ! ! // cleanup that needs to run when application is shut down
! ! ! }
! ! };
! ! // example for project-specific stuff
! ! taskManager = new BackgroundTaskManager();
! ! taskManager.newRecurringTask( new MySystemState.SystemStateUpdaterTask(), 60 );
! ! super.didFinishLaunching();
! ! log.info( "######### post-startup sequence complete #########" );
! }
Application.java (1/2)
Samstag, 22. Juni 13
37. ! // instead of using a Property, this switches gzip on/off based on system type
! @Override public boolean responseCompressionEnabled() {
! ! switch( systemType() ) {
! ! ! case TESTING! ! : return true;
! ! ! case DEVELOPMENT! : return true;
! ! default!! ! ! : return false; // gzip done by load balancer
! ! }
! }
! @Override protected void migrationsWillRun( ERXMigrator migrator ) {
! ! log.info( "Starting migrations" );
! }
!
! @Override protected void migrationsDidRun( ERXMigrator migrator ) {
! ! log.info( "Finished migrations" );
! }
}
Application.java (2/2)
Samstag, 22. Juni 13
38. ! // instead of using a Property, this switches gzip on/off based on system type
! @Override public boolean responseCompressionEnabled() {
! ! switch( systemType() ) {
! ! ! case TESTING! ! : return true;
! ! ! case DEVELOPMENT! : return true;
! ! default!! ! ! : return false; // gzip done by load balancer
! ! }
! }
! // the default context logging for exceptions is a bit too bulky for my taste, so strip that down a bit
! @Override public NSMutableDictionary extraInformationForExceptionInContext( Exception e, WOContext context ) {
! ! NSMutableDictionary<String,Object> extraInfo = ERXRuntimeUtilities.informationForException( e );
! ! // copy informatinForContext() from ERXApplication, override and strip down
! ! extraInfo.addEntriesFromDictionary( informationForContext( context ) );
! ! extraInfo.addEntriesFromDictionary( ERXRuntimeUtilities.informationForBundles() );
! ! return extraInfo;
! }
! @Override protected void migrationsWillRun( ERXMigrator migrator ) {
! ! log.info( "Starting migrations" );
! }
!
! @Override protected void migrationsDidRun( ERXMigrator migrator ) {
! ! log.info( "Finished migrations" );
! }
}
Application.java (2/2)
Samstag, 22. Juni 13
39. Session.java
• Requirement: subclass ERXSession
• No code to show, nothing special to adapt
• Except when you had used MultiECLockManager
• If you did and you want to switch to ERXEC autolocking,
remove any code related to MultiECLockManager from your
Session class
Samstag, 22. Juni 13
40. MyEditingContext.java
• Recommendation: subclass ERXEC
• You need to implement a factory
• You can have multiple factories, like one that produces
autolocking contexts, and another for manual locking, without
having different classes.
Samstag, 22. Juni 13
48. autolock vs. manual
• In general, autolocking is the right choice for almost everything
• Consider using manual locking for db-intensive background tasks
• Main autolocking tradeoff: looots of lock/unlock calls that could
become a significant overhead, depending on what you‘re doing
Samstag, 22. Juni 13
51. ERXGenericRecord
• Requirement: subclass ERXGenericRecord
• With EOGenerator, simply change your template‘s superclass
declaration and import statements
• Of course you can change them to your own MyGenericRecord
class instead, and let that extend ERXGenericRecord
• If you had a delegate in your EC to do stuff before saves, you can
now use ERXGenericRecord.willUpdate() and .willInsert()
instead
Samstag, 22. Juni 13
52. EO Templates
• Use Wonder templates from WOLips
• There‘s a wiki page with all sorts of alternatives
• In any case, take one that defines proper ERXKey constants
Samstag, 22. Juni 13
65. Information sources
• Wiki page „Project Wonder Installation“
• Wiki page „Integrate Wonder into an Existing Application“
• Wiki page „EOGenerator Templates and Additions“
• Wiki page „UTF-8 Encoding Tips“
• projectlombok.org
Samstag, 22. Juni 13