MatrixBerryCore
AJavaStaticPathMgr.m
Go to the documentation of this file.
2  properties (Constant)
3  JAVA_CLASS_PATH_TXT='javaclasspath.txt'
4  end
5  properties (Access=protected)
6  classPathFileName
7  end
8  methods (Abstract)
9  %returns a list of jar files to be put on a static java path
10  fileNameList=getJarFileNameList(self)
11  end
12  methods (Abstract,Access=protected)
13  pathClassCMat=getRelativePathListToJars(~)
14  end
15  methods
16  function self=AJavaStaticPathMgr(classPathFileName)
17  self.classPathFileName=classPathFileName;
18  end
19  function setUp(self,isUserDirStartUpDir)
20  import mxberry.core.throwwarn;
21  import mxberry.core.throwerror;
22  if nargin<2
23  isUserDirStartUpDir=true;
24  end
25  pathClassCMat=self.getRelativePathListToJars();
26  %
27  if ~isempty(pathClassCMat)
28  nPaths=size(pathClassCMat,1);
29  for iPath=1:nPaths
30  defClassName=pathClassCMat{iPath,2};
31  classLocDir=fileparts(which(defClassName));
32  if isempty(classLocDir)
33  throwerror('wrongInput',...
34  'cannot find class %s on Matlab path',...
35  classLocDir);
36  end
37  indLoc=regexp(classLocDir,'((\\|\/)(\@|\+))','once');
38  classLocDir=classLocDir(1:indLoc);
39  indSepVec=find(classLocDir==filesep);
40  indSep=indSepVec(end-pathClassCMat{iPath,3});
41  classLocDir=classLocDir(1:indSep-1);
42  relPath=pathClassCMat{iPath,1};
43  %
44  relPath=strrep(relPath,'/',filesep);
45  relPath=strrep(relPath,'\',filesep);
46  %
47  pathList=[classLocDir,filesep,relPath];
48  addpath(pathList);
49  end
50  end
51  %
52  jarFileNameList=self.getJarFileNameList();
53  toAddPathList=cellfun(@which,jarFileNameList,...
54  'UniformOutput',false);
55  %
56  isEmptyVec=cellfun('isempty',toAddPathList);
57  if any(isEmptyVec)
58  if isUserDirStartUpDir
59  fThrow=@throwerror;
60  else
61  fThrow=@throwwarn;
62  end
63  fThrow('noJarFilesOnMatlabPath',...
64  'Files %s cannot be found on Matlab path',...
65  list2str(jarFileNameList(isEmptyVec)));
66  end
67  toAddPathList=toAddPathList(~isEmptyVec);
68  %
69  if verLessThan('matlab', '8.2')
70  throwwarn('wrongMatlabVer',...
71  'Matlab version 8.2/2013b or higher is expected');
72  javaaddpath(toAddPathList);
73  else
74  if ~self.isUserDirClassPathSet()
75  missingPathList=toAddPathList;
76  isnThereVec=true(size(toAddPathList));
77  delPathList={};
78  else
79  existingUserPathList=self.readUserDirClassPath();
80  isnThereVec=~ismember(toAddPathList,existingUserPathList);
81  missingPathList=toAddPathList(isnThereVec);
82  delPathList=setdiff(existingUserPathList,toAddPathList);
83  end
84  %
85  fullStaticPathList=javaclasspath('-static');
86  isnFullThereVec=~ismember(toAddPathList,fullStaticPathList);
87  if ~any(isnFullThereVec)
88  self.dispMsg(...
89  sprintf(['files %s are already on ',...
90  'static java classpath'],...
91  list2str(toAddPathList)));
92  end
93  %
94  if isUserDirStartUpDir&&...
95  ~all(isnThereVec==isnFullThereVec)
96  isInFullStaticVec=~isnFullThereVec&isnThereVec;
97  isInUserStaticVec=isnFullThereVec&~isnThereVec;
98  globalClassPathFile=self.getPrefDirClassPathFileName();
99  throwerror('wrongStaticPath',...
100  ['files %s are in current static path but not\n',...
101  'in %s while files %s are in %s but not in\n',...
102  'current static path.\n',...
103  'It can be that %s file contains the same ',...
104  'entries as %s.\n',...
105  'If that is the case please remove those ',...
106  'entries and restart Matlab!'],...
107  list2str(toAddPathList(isInFullStaticVec)),...
108  self.classPathFileName,...
109  list2str(toAddPathList(isInUserStaticVec)),...
110  self.classPathFileName,globalClassPathFile,...
111  self.classPathFileName);
112  end
113  %
114  self.setUserDirClassPath(toAddPathList);
115  %
116  if any(isnFullThereVec)
117  throwwarn('restartMatlab',...
118  ['files %s have been added ',...
119  'to Java static class path,\n ',...
120  'files %s have been deleted from Java ',...
121  'static class path, \n',...
122  '<<-------------------->> \n',...
123  'adding these files to Dynamic Java Path \n',...
124  'as a temporary measure \n',...
125  '<<-------------------->> \n',...
126  ' \n',...
127  '<<<<------ PLEASE RESTART MATLAB ------->>>>'],...
128  list2str(missingPathList),list2str(delPathList));
129  %
130  javaaddpath(toAddPathList(isnFullThereVec));
131  end
132  end
133  end
134  end
135  methods (Static)
136  function pathList=getUserPathList(~)
137  staticPathList=javaclasspath('-static');
138  isUserPath=cellfun('isempty',regexp(javaclasspath('-static'),...
139  ['^',regexptranslate('escape',matlabroot)]));
140  pathList=staticPathList(isUserPath);
141  end
142  end
143  methods (Access=protected)
144  function pathList=readClassPathFile(~,fileName)
145  [fid,errMsg]=fopen(fileName,'r');
146  if fid<0
147  throwerror('cannotOpenFile',errMsg);
148  end
149  %
150  try
151  resCell=textscan(fid,'%s','Delimiter','\n');
152  pathList=resCell{1};
153  catch meObj
154  fclose(fid);
155  rethrow(meObj);
156  end
157  fclose(fid);
158  end
159  function pathList=parseClassPathStr(~,classPathStr)
160  resCell=textscan(classPathStr,'%s','Delimiter','\n');
161  pathList=resCell{1};
162  end
163  function globalClassPathFile=getPrefDirClassPathFileName(self)
164  globalClassPathFile=[prefdir,filesep,self.JAVA_CLASS_PATH_TXT];
165  end
166  end
167  methods (Access=private)
168  function isPos=isUserDirClassPathSet(self)
169  isPos=mxberry.io.isfile(...
170  self.classPathFileName,false);
171  end
172  %
173  function pathList=readUserDirClassPath(self)
174  pathList=self.readClassPathFile(self.classPathFileName);
175  end
176  %
177  function setUserDirClassPath(self,pathList)
178  import mxberry.core.throwerror;
179  [fid,errMsg]=fopen(self.classPathFileName,'w');
180  if fid<0
181  throwerror('cannotOpenFile',errMsg);
182  end
183  try
184  if ~isempty(pathList)
185  msgStr=sprintf('adding %s to %s',...
186  list2str(pathList),self.classPathFileName);
187  fwrite(fid,mxberry.core.string.catwithsep(pathList,...
188  sprintf('\n')));
189  self.dispMsg([msgStr,' :done'])
190  else
191  self.dispMsg(sprintf('Nothing to add to %s',...
192  self.classPathFileName));
193  end
194  catch meObj
195  fclose(fid);
196  rethrow(meObj);
197  end
198  fclose(fid);
199  end
200  function dispMsg(self,msgStr)
201  SEP_SYMB='-';
202  SEP_LENGTH=70;
203  prefixStr=class(self);
204  remSep=repmat(SEP_SYMB,1,max(SEP_LENGTH-numel(prefixStr),0));
205  %
206  nMid=fix(numel(remSep)*0.5);
207  fprintf([remSep(1:nMid),prefixStr,remSep((nMid+1):end),'\n']);
208  disp(msgStr);
209  fprintf(['\n',repmat(SEP_SYMB,1,SEP_LENGTH),'\n']);
210  %
211  end
212  end
213 end
214 %
215 function outStr=list2str(inpList)
216 outStr=sprintf('\n[%s]\n',mxberry.core.string.catwithsep(inpList,sprintf(',\n')));
217 end
function filesep()
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function repmat(in inpArray, in varargin)
function throwwarn(in msgTag, in varargin)
THROWWARN works similarly to built-in WARNING function in case when there is no output arguments but ...
function isfile(in fileName, in isJavaBased)
ISFILE returns true if a specified name corresponds to an existing file.
function ismember(in leftVec, in rightVec, in varargin)
ISMEMBER - ismember implementation for arrays of any type.
function catwithsep(in inpStrList, in sepStr)
CATWITHSEP concatenates input cell array of strings inserting a specified separator between the strin...