| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- //
- // System.Web.UI.Page.jvm.cs
- //
- // Authors:
- // Eyal Alaluf ([email protected])
- //
- // (C) 2006 Mainsoft Co. (http://www.mainsoft.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using javax.servlet.http;
- using System.Collections.Specialized;
- using System.Globalization;
- using System.Web.Hosting;
- using System.Web.J2EE;
- using System.ComponentModel;
- using System.IO;
- using javax.faces.context;
- using javax.faces.render;
- using javax.servlet;
- using javax.faces;
- using javax.faces.application;
- using javax.faces.@event;
- using javax.faces.el;
- using javax.faces.component;
- using System.Threading;
- using System.Web.Configuration;
- namespace System.Web.UI
- {
- public partial class Page
- {
- string _namespace = null;
- StateManager.SerializedView _facesSerializedView;
- MethodBinding _action;
- MethodBinding _actionListener;
- bool _immediate;
- Pair _state;
- bool [] _validatorsState;
- ICallbackEventHandler _callbackTarget;
- string _callbackEventError = String.Empty;
- static readonly object CrossPagePostBack = new object ();
- FacesContext _facesContext;
- static readonly java.util.List emptyList = java.util.Collections.unmodifiableList (new java.util.ArrayList ());
- bool _isMultiForm = false;
- bool _isMultiFormInited = false;
- internal string Namespace
- {
- get {
- if (_namespace == null) {
- if (getFacesContext () != null) {
- _namespace = getFacesContext ().getExternalContext ().encodeNamespace (String.Empty);
- }
- _namespace = _namespace ?? String.Empty;
- }
- return _namespace;
- }
- }
- internal string theForm {
- get {
- return "theForm" + Namespace;
- }
- }
- internal bool IsMultiForm {
- get {
- if (!_isMultiFormInited) {
- string isMultiForm = WebConfigurationManager.AppSettings ["mainsoft.use.portlet.namespace"];
- _isMultiForm = isMultiForm != null ? Boolean.Parse(isMultiForm) : false;
- _isMultiFormInited = true;
- }
- return _isMultiForm;
- }
- }
- IHttpHandler EnterThread () {
- IHttpHandler jsfHandler = _context.CurrentHandler;
- _context.PopHandler ();
- _context.PushHandler (this);
- if (jsfHandler == _context.Handler)
- _context.Handler = this;
- return jsfHandler;
- }
- void ExitThread (IHttpHandler jsfHandler) {
- // TODO
- //if (context.getResponseComplete ())
- // Response.End ();
- _context.PopHandler ();
- _context.PushHandler (jsfHandler);
- if (this == _context.Handler)
- _context.Handler = jsfHandler;
- }
- public override void encodeBegin (FacesContext context) {
- // do nothing
- }
- public override void encodeChildren (FacesContext context) {
- System.Diagnostics.Trace.WriteLine ("encodeChildren");
- // reset _facesContext if changed between action and render phases (such portal).
- _facesContext = null;
- IHttpHandler jsfHandler = EnterThread ();
- bool wasException = false;
- try {
- if (!context.getResponseComplete ()) {
- if (IsCallback) {
- string result = ProcessGetCallbackResult (_callbackTarget, _callbackEventError);
- HtmlTextWriter callbackOutput = new HtmlTextWriter (Response.Output);
- callbackOutput.Write (result);
- callbackOutput.Flush ();
- return;
- }
- // ensure lifecycle complete.
- if (!IsLoaded) {
- ProcessLoad ();
- RestoreValidatorsState (_validatorsState);
- }
- if (!IsPrerendered)
- ProcessLoadComplete ();
- RenderPage ();
- }
- }
- catch (Exception ex) {
- wasException = true;
- HandleException (ex);
- }
- finally {
- try {
- if (!wasException)
- ProcessUnload ();
- }
- finally {
- ExitThread (jsfHandler);
- }
- }
- }
- public override void encodeEnd (FacesContext context) {
- // do nothing
- }
- // BUGBUG: must return correct value. Currently returns 0 as performance optimization.
- public override int getChildCount ()
- {
- return 0;
- }
- // BUGBUG: must return correct value. Currently returns empty list as performance optimization.
- public override java.util.List getChildren ()
- {
- return emptyList;
- }
- public override UIComponent getParent () {
- return null;
- }
- public override void setParent (UIComponent parent) {
- //ignore: parent is root
- }
- // TODO: consider validators state
- public override object processSaveState (FacesContext context) {
- System.Diagnostics.Trace.WriteLine ("processSaveState");
- object state = new Pair (_state, GetValidatorsState ());
- if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
- int length;
- byte [] buffer = new ObjectStateFormatter (this).SerializeInternal (state, out length);
- if (buffer.Length != length) {
- byte [] trimmedBuffer = new byte [length];
- Array.Copy (buffer, trimmedBuffer, length);
- buffer = trimmedBuffer;
- }
- state = vmw.common.TypeUtils.ToSByteArray (buffer);
- }
- return state;
- }
- public override void processRestoreState (FacesContext context, object state) {
- System.Diagnostics.Trace.WriteLine ("processRestoreState");
- if (state == null)
- throw new ArgumentNullException ("state");
- IHttpHandler jsfHandler = EnterThread ();
- try {
- if (getFacesContext ().getApplication ().getStateManager ().isSavingStateInClient (getFacesContext ())) {
- byte [] buffer = (byte []) vmw.common.TypeUtils.ToByteArray ((sbyte []) state);
- state = new ObjectStateFormatter (this).DeserializeInternal (buffer);
- }
- _state = (Pair) ((Pair) state).First;
- _validatorsState = (bool []) ((Pair) state).Second;
- RestorePageState ();
- }
- catch (Exception ex) {
- HandleException (ex);
- }
- finally {
- ExitThread (jsfHandler);
- }
- }
- public override void processDecodes (FacesContext context) {
- System.Diagnostics.Trace.WriteLine ("processDecodes");
- IHttpHandler jsfHandler = EnterThread ();
- try {
- ProcessPostData ();
- EventRaiserFacesEvent facesEvent = new EventRaiserFacesEvent (this);
- facesEvent.setPhaseId (PhaseId.INVOKE_APPLICATION);
- context.getViewRoot ().queueEvent (facesEvent);
- base.processDecodes (context);
- }
- catch (Exception ex) {
- HandleException (ex);
- }
- finally {
- ExitThread (jsfHandler);
- }
- }
- public override void processValidators (FacesContext context) {
- System.Diagnostics.Trace.WriteLine ("processValidators");
- IHttpHandler jsfHandler = EnterThread ();
- try {
- base.processValidators (context);
- }
- catch (Exception ex) {
- HandleException (ex);
- }
- finally {
- ExitThread (jsfHandler);
- }
- }
- public override void processUpdates (FacesContext context) {
- System.Diagnostics.Trace.WriteLine ("processUpdates");
- IHttpHandler jsfHandler = EnterThread ();
- try {
- base.processUpdates (context);
- }
- catch (Exception ex) {
- HandleException (ex);
- }
- finally {
- ExitThread (jsfHandler);
- }
- }
- public override void broadcast (FacesEvent e) {
- System.Diagnostics.Trace.WriteLine ("broadcast");
- if (!(e is EventRaiserFacesEvent))
- throw new NotSupportedException ("FacesEvent of class " + e.GetType ().Name + " not supported by Page");
- IHttpHandler jsfHandler = EnterThread ();
- bool doUnload = false;
- try {
- ProcessRaiseEvents ();
- doUnload = (ProcessLoadComplete () && IsCrossPagePostBack);
- }
- catch (Exception ex) {
- doUnload = false;
- HandleException (ex);
- }
- finally {
- try {
- if (doUnload) {
- getFacesContext ().responseComplete ();
- ProcessUnload ();
- }
- }
- finally {
- ExitThread (jsfHandler);
- }
- }
- }
- void HandleException (Exception ex) {
- try {
- if (ex is ThreadAbortException) {
- if (FlagEnd.Value == ((ThreadAbortException) ex).ExceptionState) {
- Thread.ResetAbort ();
- return;
- }
- vmw.common.TypeUtils.Throw (ex);
- }
- else
- ProcessException (ex);
- }
- finally {
- if (getFacesContext () != null)
- getFacesContext ().responseComplete ();
- ProcessUnload ();
- }
- }
- bool [] GetValidatorsState () {
- if (is_validated && Validators.Count > 0) {
- bool [] validatorsState = new bool [Validators.Count];
- bool isValid = true;
- for (int i = 0; i < Validators.Count; i++) {
- IValidator val = Validators [i];
- if (!val.IsValid)
- isValid = false;
- else
- validatorsState [i] = true;
- }
- return validatorsState;
- }
- return null;
- }
- void RestoreValidatorsState (bool [] validatorsState) {
- if (validatorsState == null)
- return;
- is_validated = true;
- for (int i = 0; i < Math.Min (validatorsState.Length, Validators.Count); i++) {
- IValidator val = Validators [i];
- val.IsValid = validatorsState [i];
- }
- }
- ResponseWriter SetupResponseWriter (TextWriter httpWriter) { //TODO
- FacesContext facesContext = getFacesContext ();
- ResponseWriter oldWriter = facesContext.getResponseWriter ();
- if (oldWriter == null)
- throw new InvalidOperationException ();
- ResponseWriter writer = oldWriter.cloneWithWriter (new AspNetResponseWriter (httpWriter));
-
- facesContext.setResponseWriter (writer);
- return oldWriter;
- }
- string DecodeNamespace (string id) {
- if (Namespace.Length > 0 && id.Length > Namespace.Length && id.StartsWith (Namespace, StringComparison.Ordinal))
- id = id.Substring (Namespace.Length);
- return id;
- }
- protected override FacesContext getFacesContext () {
- return _facesContext ?? (_facesContext = FacesContext.getCurrentInstance ());
- }
- #region FacesPageStatePersister
- sealed class FacesPageStatePersister : PageStatePersister
- {
- public FacesPageStatePersister (Page page)
- : base (page) {
- }
- public override void Load () {
- if (Page._state != null) {
- ViewState = Page._state.First;
- ControlState = Page._state.Second;
- }
- }
- public override void Save () {
- if (ViewState != null || ControlState != null)
- Page._state = new Pair (ViewState, ControlState);
- }
- }
- #endregion
- #region EventRaiserFacesEvent
- sealed class EventRaiserFacesEvent : FacesEvent
- {
- public EventRaiserFacesEvent (Page page)
- : base (page) {
- }
- public override bool isAppropriateListener (FacesListener __p1) {
- throw new NotSupportedException ();
- }
- public override void processListener (FacesListener __p1) {
- throw new NotSupportedException ();
- }
- }
- #endregion
- #region AspNetResponseWriter
- private sealed class AspNetResponseWriter : java.io.Writer
- {
- readonly TextWriter _writer;
- public AspNetResponseWriter (TextWriter writer) {
- _writer = writer;
- }
- public override void close () {
- _writer.Close ();
- }
- public override void flush () {
- _writer.Flush ();
- }
- public override void write (char [] __p1, int __p2, int __p3) {
- _writer.Write (__p1, __p2, __p3);
- }
- public override void write (int __p1) {
- _writer.Write ((char) __p1);
- }
- public override void write (char [] __p1) {
- _writer.Write (__p1);
- }
- public override void write (string __p1) {
- _writer.Write (__p1);
- }
- public override void write (string __p1, int __p2, int __p3) {
- _writer.Write (__p1, __p2, __p3);
- }
- }
- #endregion
- }
- }
|