1 21 22 package org.opensubsystems.patterns.listdata.util; 23 24 import java.util.List ; 25 import java.util.Properties ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import org.opensubsystems.core.error.OSSException; 30 import org.opensubsystems.core.logic.ControllerManager; 31 import org.opensubsystems.core.util.Config; 32 import org.opensubsystems.core.util.GlobalConstants; 33 import org.opensubsystems.core.util.Log; 34 import org.opensubsystems.patterns.listdata.data.ListOptions; 35 import org.opensubsystems.patterns.listdata.logic.ListController; 36 37 51 public class AsyncDataLoader extends Thread 52 { 53 55 60 public static final String ASYNC_LOADER_PAGE_SIZE = "oss.listdata.asyncloader.pagesize"; 61 62 64 68 public static final int ASYNC_LOADER_PAGE_SIZE_DEFAULT = 50; 70 71 75 public static final Runnable NOTHING_TO_DO = new Runnable () 76 { 77 public void run() 78 { 79 } 80 }; 81 82 84 88 protected int m_iLoaderIdentification; 89 90 93 protected AsyncDataLoaderOwner m_owner; 94 95 98 protected Class m_clsListFactoryClassName; 99 100 106 protected int m_iCurrentObjectVersion; 107 108 110 113 private static Logger s_logger = Log.getInstance(AsyncDataLoader.class); 114 115 120 protected static int s_iPageSize; 121 122 124 127 static 128 { 129 Properties prpSettings; 131 132 prpSettings = Config.getInstance().getPropertiesSafely(); 133 s_iPageSize = Config.getIntPropertyInRange( 134 prpSettings, 135 ASYNC_LOADER_PAGE_SIZE, 136 ASYNC_LOADER_PAGE_SIZE_DEFAULT, 137 "Asynchronous data loader page size", 138 0, Integer.MAX_VALUE); 140 } 141 142 156 public AsyncDataLoader( 157 int iLoaderIdentification, 158 AsyncDataLoaderOwner owner, 159 Class clsListFactoryClassName, 160 int iObjectVersion 161 ) 162 { 163 if (GlobalConstants.ERROR_CHECKING) 164 { 165 assert owner != null : "Owner of the data loader cannot be null."; 166 assert clsListFactoryClassName != null : "List factory class cannot be null."; 167 } 168 169 m_iLoaderIdentification = iLoaderIdentification; 170 m_owner = owner; 171 m_clsListFactoryClassName = clsListFactoryClassName; 172 m_iCurrentObjectVersion = iObjectVersion; 173 } 174 175 177 180 public void run( 181 ) 182 { 183 ListController listLoader; 184 ListOptions listData = getInitialListOptions(); 185 Object [] data; 186 List lstData; 187 188 try 189 { 190 listLoader = getListController(); 191 data = listLoader.getShowList(listData); 192 if (data != null) 193 { 194 listData = (ListOptions)data[0]; 195 lstData = (List )data[1]; 196 197 while ((listData != null) && (lstData != null) && (!lstData.isEmpty())) 198 { 199 if (stopLoading()) 206 { 207 break; 208 } 209 processData(lstData, m_iCurrentObjectVersion); 210 211 if (stopLoading()) 212 { 213 break; 214 } 215 else 216 { 217 if (listData.getEndPosition() != listData.getActualListSize()) 218 { 219 data = listLoader.getNextPage(listData); 220 if (data == null) 221 { 222 break; 223 } 224 else 225 { 226 listData = (ListOptions)data[0]; 227 lstData = (List )data[1]; 228 } 229 } 230 else 231 { 232 break; 234 } 235 } 236 } 237 238 allDataLoaded(); 239 } 240 } 241 catch (Throwable thr) 242 { 243 s_logger.log(Level.WARNING, 244 "Unexpected error has occured while loading data.", thr); 245 } 246 } 247 248 250 256 protected ListOptions getInitialListOptions( 257 ) 258 { 259 ListOptions options = new ListOptions(m_clsListFactoryClassName); 260 options.setPageSize(s_iPageSize); 261 options = m_owner.getInitialListOptions(this, options); 263 264 return options; 265 } 266 267 273 protected boolean stopLoading( 274 ) 275 { 276 return m_owner.stopLoading(this); 277 } 278 279 289 protected void processData( 290 final List lstDataToAdd, 291 final int iOriginalObjectVersion 292 293 ) 294 { 295 m_owner.processData(this, lstDataToAdd, iOriginalObjectVersion); 296 } 297 298 302 protected void allDataLoaded() 303 { 304 m_owner.allDataLoaded(this); 305 } 306 307 313 protected ListController getListController( 314 ) throws OSSException 315 { 316 ListController controller; 317 318 controller = (ListController)ControllerManager.getInstance( 319 ListController.class); 320 321 return controller; 322 } 323 } 324 | Popular Tags |