PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/dotnet/fanx/util/EnvProps.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 123 lines | 92 code | 14 blank | 17 comment | 20 complexity | 3c9fd2294a5f13b8a93fb398f13bbaac MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2009, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 21 Jul 09 Brian Frank Original Repo code
  7. // 28 Jan 10 Brian Frank Split out into Env helper class
  8. //
  9. using System.Collections;
  10. using System.Runtime.CompilerServices;
  11. using Fan.Sys;
  12. namespace Fanx.Util
  13. {
  14. public class EnvProps
  15. {
  16. public EnvProps(Env env) { this.m_env = env; }
  17. [MethodImpl(MethodImplOptions.Synchronized)]
  18. public Map get(Pod pod, Uri uri, Duration maxAge)
  19. {
  20. Key key = new Key(pod, uri);
  21. CachedProps cp = (CachedProps)m_cache[key];
  22. if (cp == null || Duration.nowTicks() - cp.m_read > maxAge.m_ticks)
  23. cp = refresh(key, cp);
  24. return cp.m_props;
  25. }
  26. private CachedProps refresh(Key key, CachedProps cp)
  27. {
  28. List files = m_env.findAllFiles(Uri.fromStr("etc/" + key.m_pod + "/" + key.m_uri));
  29. if (cp != null && !cp.isStale(files)) return cp;
  30. if (key.m_uri.isPathAbs()) throw ArgErr.make("Env.props Uri must be relative: " + key.m_uri).val;
  31. cp = new CachedProps(key, files);
  32. m_cache[key] = cp;
  33. return cp;
  34. }
  35. static Map readDef(Pod pod, Uri uri)
  36. {
  37. uri = Uri.fromStr(pod.uri() + "/" + uri);
  38. Fan.Sys.File f = (Fan.Sys.File)pod.file(uri, false);
  39. Map map = Sys.m_emptyStrStrMap;
  40. try
  41. {
  42. if (f != null) map = (Map)f.readProps().toImmutable();
  43. }
  44. catch (System.Exception e)
  45. {
  46. System.Console.WriteLine("ERROR: Cannot load props " + pod + "::" + uri);
  47. System.Console.WriteLine(" " + e);
  48. }
  49. return map;
  50. }
  51. static Map read(Map defProps, Key key, List files)
  52. {
  53. if (files.isEmpty()) return defProps;
  54. Map acc = defProps.dup();
  55. for (int i=files.sz()-1; i>=0; --i)
  56. {
  57. InStream input = ((File)files.get(i)).@in();
  58. try { acc.setAll(input.readProps()); }
  59. finally { input.close(); }
  60. }
  61. return (Map)acc.toImmutable();
  62. }
  63. //////////////////////////////////////////////////////////////////////////
  64. // Key
  65. //////////////////////////////////////////////////////////////////////////
  66. class Key
  67. {
  68. public Key(Pod p, Uri u) { m_pod = p; m_uri = u; }
  69. public override int GetHashCode() { return m_pod.GetHashCode() ^ m_uri.GetHashCode(); }
  70. public override bool Equals(object o) { Key x = (Key)o; return m_pod == x.m_pod && m_uri == x.m_uri; }
  71. public Pod m_pod;
  72. public Uri m_uri;
  73. }
  74. //////////////////////////////////////////////////////////////////////////
  75. // CachedProps
  76. //////////////////////////////////////////////////////////////////////////
  77. class CachedProps
  78. {
  79. public CachedProps(Key key, List files)
  80. {
  81. this.m_files = files;
  82. this.m_modified = new long[files.sz()];
  83. for (int i=0; i<files.sz(); ++i)
  84. this.m_modified[i] = ((File)files.get(i)).modified().ticks();
  85. this.m_defProps = readDef(key.m_pod, key.m_uri);
  86. this.m_props = read(m_defProps, key, files);
  87. this.m_read = Duration.nowTicks();
  88. }
  89. public bool isStale(List x)
  90. {
  91. if (m_files.sz() != x.sz()) return true;
  92. for (int i=0; i<x.sz(); ++i)
  93. if (m_modified[i] != ((File)x.get(i)).modified().ticks())
  94. return true;
  95. return false;
  96. }
  97. public long m_read; // Duration.nowTicks when we read
  98. public List m_files; // list of files we read from
  99. public long[] m_modified; // timestamps of file when we read
  100. public Map m_defProps; // props defined in pod resource (immutable)
  101. public Map m_props; // immutable props read
  102. }
  103. //////////////////////////////////////////////////////////////////////////
  104. // Fields
  105. //////////////////////////////////////////////////////////////////////////
  106. private Env m_env;
  107. private Hashtable m_cache = new Hashtable();
  108. }
  109. }