@@ -22,26 +22,22 @@ public class CliContext {
2222 private Application application ;
2323 private TmcCore tmcCore ;
2424 private Settings settings ;
25+
26+ /* cached values */
2527 private boolean hasLogin ;
2628 private CourseInfo courseInfo ;
27-
2829 private HashMap <String , String > properties ;
2930 private final boolean inTest ;
3031
3132 /*TODO some of the constructors could be removed */
3233 public CliContext (Io io ) {
33- this (io , new WorkDir ());
34+ this (io , new WorkDir (), null );
3435 }
3536
3637 public CliContext (Io io , TmcCore core ) {
3738 this (io , new WorkDir (), core );
3839 }
3940
40- @ Deprecated
41- public CliContext (Io io , WorkDir workDir ) {
42- this (io , workDir , null );
43- }
44-
4541 public CliContext (Io io , WorkDir workDir , TmcCore core ) {
4642 inTest = (io != null );
4743 if (!inTest ) {
@@ -60,6 +56,11 @@ public CliContext(Io io, WorkDir workDir, TmcCore core) {
6056 this .courseInfo = null ;
6157 }
6258
59+ /*TODO create reset method for removing all cached data that is called
60+ * when working directory is changed or by users demand also use it in
61+ * constructor.
62+ */
63+
6364 protected void setApp (Application app ) {
6465 this .application = app ;
6566 }
@@ -85,16 +86,6 @@ public WorkDir getWorkDir() {
8586 return this .workDir ;
8687 }
8788
88- /**
89- * Does some thing in old style.
90- *
91- * @deprecated use {@link fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir#setWorkdir(String)} instead.
92- */
93- @ Deprecated
94- public void setWorkdir (WorkDir workDir ) {
95- this .workDir = workDir ;
96- }
97-
9889 public CourseInfo createCourseInfo (Course course ) {
9990 return new CourseInfo (settings , course );
10091 }
@@ -109,6 +100,24 @@ public Boolean saveProperties() {
109100 return SettingsIo .saveProperties (properties );
110101 }
111102
103+ public CourseInfo getCourseInfo () {
104+ if (courseInfo != null ) {
105+ return courseInfo ;
106+ }
107+ if (workDir .getConfigFile () == null ) {
108+ return null ;
109+ }
110+ courseInfo = CourseInfoIo .load (workDir .getConfigFile ());
111+ if (courseInfo == null ) {
112+ io .println ("Course configuration file "
113+ + workDir .getConfigFile ().toString ()
114+ + "is invalid." );
115+ //TODO add a way to rewrite the course config file.
116+ return null ;
117+ }
118+ return this .courseInfo ;
119+ }
120+
112121 // Method is used to help testing
113122 public void setTmcCore (TmcCore tmcCore ) {
114123 this .tmcCore = tmcCore ;
@@ -168,23 +177,18 @@ private void createTmcCore(Settings settings) {
168177 }
169178
170179 private boolean createTmcCore () {
171- Settings settings ;
180+ Settings cachedSettings = null ;
172181
173182 if (workDir .getConfigFile () != null ) {
174183 // If we're in a course directory, we load settings matching the course
175184 // Otherwise we just load the last used settings
176- courseInfo = CourseInfoIo .load (workDir .getConfigFile ());
177- if (courseInfo == null ) {
178- io .println ("Course configuration file "
179- + workDir .getConfigFile ().toString ()
180- + "is invalid." );
181- //TODO add a way to rewrite the course config file.
182- return false ;
185+ courseInfo = getCourseInfo ();
186+ if (courseInfo != null ) {
187+ cachedSettings = SettingsIo .load (courseInfo .getUsername (),
188+ courseInfo .getServerAddress ());
183189 }
184- settings = SettingsIo .load (courseInfo .getUsername (),
185- courseInfo .getServerAddress ());
186190 } else {
187- settings = SettingsIo .load ();
191+ cachedSettings = SettingsIo .load ();
188192 }
189193
190194 hasLogin = true ;
@@ -193,7 +197,7 @@ private boolean createTmcCore() {
193197 settings = new Settings ();
194198 }
195199
196- createTmcCore (settings );
200+ createTmcCore (cachedSettings );
197201 return true ;
198202 }
199203}
0 commit comments